Niel de Wet
Niel de Wet

Reputation: 8416

GwtMockito: Is there a way to test Window.open()?

I would like to test that my call of Window.open(String) is with the correct URL (to download a file).

Is there any better way of doing it, besides using a partial mock, like this?

MySUT sut = Mockito.spy(new MySUT());
String expectedURL = "http://www.example.com";

doNothing().when(sut).openWindow(expectedURL);

sut.doSomethingThatOpensURL();

verify(sut).openWindow(expectedURL);

Where openWindow(String) is as simple as possible in MySUT:

void openWindow(String url) {
    Window.open(url);
}

Does GwtMockito give you something to test methods that execute native javascript like Window.open(String)?

Upvotes: 0

Views: 599

Answers (1)

xRomZak
xRomZak

Reputation: 185

I suppose there is no ability to verify it using GwtMockito. But I believe it's possible using PowerMock and EasyMock. This mocking frameworks provide great flexibility to test static methods and 3rd party classes.

I've just created a simple example how you can test Window.open(). In my test case I'm just checking Window.alert(), for example.

@RunWith(PowerMockRunner.class)
@PrepareForTest(Window.class)
@SuppressStaticInitializationFor("com.google.gwt.user.client.Window")
public class SimpleTester {

  /** Simple class for test. */
  private class TestClass {
    public void open(String url) {
      Window.alert(url);
    }
  }

  @Test
  public void test() {
    PowerMock.mockStatic(Window.class);

    TestClass obj = new TestClass();
    Window.alert("test");
    EasyMock.expectLastCall();
    PowerMock.replay(Window.class);

    obj.open("test");
    PowerMock.verify(Window.class);
  }
}

Upvotes: 0

Related Questions