Hot questions for Using Mockito in gwt

Question:

I'm learning GwtMockito but having trouble getting consistent verify() method results in one of my tests.

I'm trying to test the correct GwtEvents are being fired by my application. So I've mocked the Event Bus like this in my @Before method:

eventBus = mock(HandlerManager.class);

This test passes as expected:

// Passes as expected
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));

I wanted to force the test to fail just to know it was running correctly. So I changed it to this and it still passes:

// Expected this to fail, but it passes
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
verifyZeroInteractions(eventBus).fireEvent(any(ErrorOccurredEvent.class));

This seems contradictory to me. So I removed the first test:

// Fails as expected
verifyZeroInteractions(eventBus).fireEvent(any(ErrorOccurredEvent.class));

Finally I added an unrelated event that should cause it to fail

// Expected to fail, but passes
verify(eventBus).fireEvent(any(ErrorOccurredEvent.class));
verify(eventBus).fireEvent(any(ModelCreatedEvent.class));  // This event is not used at all by the class that I'm testing. It's not possible for it to be fired.

I'm not finding any documentation that explains what's going on. Both ErrorOccurredEvent and ModelCreatedEvent extend GwtEvent, and have been verified in manual testing. Am I testing my EventBus incorrectly? If so, what is a better way to go about it?

Update

I've done some additional experimenting. It appears to be an issue I'm having with the Mockito matcher. When I get the test to fail the exception reports the method signature as eventBus.fireEvent(<any>) so it doesn't appear to be taking into account the different classes I'm passing into the any method. Not sure what to do about this yet, but including it here for anyone else researching this problem.


Answer:

The method you're looking for is isA, instead of any.

Question:

I'm doing a project with gwt at the moment. I have to test a presenter using Eclipse Luna, JUnit and Mockito and have problems doing anything.

After I failed with mockito I discovered GwtMockito and tried that. I used this example to get started: Github GwtMockito example

My Test class looks like this:

@RunWith(GwtMockitoTestRunner.class)
public class GamePresenterTest {

@GwtMock private GameServiceAsync gameService;
@GwtMock private LobbyPresenter lobbyPresenter;
@GwtMock private EventBus eventBus;
@GwtMock private Display gameView;
@GwtMock private User user;
@GwtMock private Player player;
@GwtMock private ClickEvent event;

private GamePresenter presenter;
private String username = "name";
private String gameTitle = "title";

@Before
public void setUp() {
    System.out.println("TODO: set up");
}

@Test
public void testActionTableClicked() {
    System.out.println("TODO: add test");
}
}

When I run it I get this

java.lang.NoClassDefFoundError: javassist/NotFoundException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getConstructor(Class.java:1825)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: javassist.NotFoundException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 18 more

When I remove the @RunWith(GwtMockitoTestRunner.class) I get another message that tells me I should use initMocks, but I don't how to do that, too. Without @RunWith(GwtMockitoTestRunner.class) and the @GwtMock stuff everything works fine of course.

I hope someone can help, because my project ends in a few weeks.


Answer:

Looks like you need javassist-<version>.jar. Add it to your classpath and it should work