Sunday, March 24, 2013

How to mock static client side DOM calls with PowerMock?

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+unsubscribe@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.
 
 

No comments:

Post a Comment