Question

That's my JMock test:

@Test
public void testRunModuleOK() {
    App instance = getInstanceApp(window,logger);
    Mockery context = new JUnit4Mockery();
    final Modulable m = context.mock(Modulable.class);

    context.checking(new Expectations() {{
        oneOf(m).init(logger);
        oneOf(m).start();
    }});

    instance.runModule(m);

}

And that's runModule:

void runModule(Modulable module) {
    if(module == null) 
        throw new NullPointerException("Module is null");
    module.init(mainLogger);
    runningModules.add(module);
    module.start();
}

I want my test to make sure that init() and start() is always called once. Unfortunately, when I comment out module.init and module.start in runModule, test still passes. I used println to make sure the code and the test is called. Strangely, when I call init (or start) twice, I get failure as expected. Also, when I comment out 'oneOf...' from expectations, I also get failure. Why does the test passes if module.init and / or module.start is not called in runModule? Thanks.

Was it helpful?

Solution

You're missing the verify step that tells JMock that your test is finished and it should close off any outstanding checks. Call assertIsSatisfied() on your mock context.

Alternatively, switch from using Mockery context = new JUnit4Mockery(); to using @Rule public JUnitRuleMockery context = new JUnitRuleMockery();. This will cut out some of that sort of boilerplate for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top