Re: How to mock static client side DOM calls with PowerMock?

2013-03-24 Thread Jens
The DOM class contains a static class variable "impl" that is instantiated 
using GWT.create(). I don't think you can workaround this fact using 
PowerMockito.

What you can do is to refactor your code slightly. Instead of

public Foo() {
  id = DOM.createUniqueId();
}

you would refactor it to

public Foo(IdGenerator idGenerator) {
  id = idGenerator.createUniqueId();
}

where IdGenerator is an interface. You can then create a default 
implementation of that interface that uses DOM.createUniqueId() in your 
production code and during testing you can then mock that interface easily.

If you don't want that, you have to use a slow GWTTestCase to make 
GWT.create() work. 

You could also move that code into a view implementation that would need a 
GWTTestCase anyways if you choose to test that view implementation.

-- J.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-web-toolkit+unsubscr...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




How to mock static client side DOM calls with PowerMock?

2013-03-24 Thread membersound
Hi,

I have a class Bar that extends Foo. In Foo there is a DOM.createUniqueId()that 
I want to mock.

What am I missing in the following code?


Foo {
String id;

public Foo() {
String id = DOM.createUniqueId();
}
}

Bar extends Foo {
public Bar() {
super();
}

public boolean testMe() {
return true;
}
}


@RunWith(PowerMockRunner.class)
@PrepareForTest(DOM.class)
Class TestFoo {
@Test
public void testFoo() {
PowerMockito.mockStatic(DOM.class);
PowerMockito.when(DOM.createUniqueId()).thenReturn("1");

Foo foo = new Foo();
assertTrue(foo.testMe());
assertEquals(bar.getId(), "1");
}
}

Result:
Caused by: java.lang.UnsupportedOperationException: ERROR: GWT.create() is 
only usable in client code!

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-web-toolkit+unsubscr...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.