Home » Java » Expecting multiple void methods with EasyMock 3.5

Expecting multiple void methods with EasyMock 3.5

Posted by: admin October 26, 2017 Leave a comment

Questions:

I am having an issue with Easymock, where I am trying to make a mock expecting a number of void calls. But I am having a problem making the mock fail when it should.

So I am expecting a call to persistConfiguration(Map) which i am absolutely sure is not being called. BUT the test passes all the same.

My mocked interface looks like this:

public abstract class MyMockedClass{
     public abstract void start() throws Exception;
     public abstract void stop() throws Exception;
     public abstract void persistConfiguration(Map<String, Object> newConfiguration) throws Exception;
}

The test looks like this

@RunWith(EasyMockRunner.class)
public class MyFailingTest{
@Mock
private MyMockedClass provider;
....
    @Before
    public void setUp() throws Exception{
         Map<String, Object> expectedSetConfig = new HashMap<>();
         expectedSetConfig.put("String", setString);
         expectedSetConfig.put("File", testFile2.getCanonicalPath());

         /*Expect*/provider.start();
         expectLastCall().atLeastOnce();
         /*Expect*/provider.persistConfiguration(expectedSetConfig);
         expectLastCall().times(1);
         /*Expect*/provider.stop();
         expectLastCall().atLeastOnce();

         replay(provider);
    }

}

If I remove the expectation provider.start() and the matching getLastCall call the test fails with the message

Unexpected method call MyMockedClass.start():
MyMockedClass.persistConfiguration({String=newString, File=C:\workspace\meh\stuff}): expected: 1, actual: 0
MyMockedClass.stop(): expected: at least 1, actual: 0

So the test CAN fail, but my question is: How do i make the test fail if either the start, stop or persistConfiguration method is called?
And the persistConfiguration method fail if it is called with anything but a map containing those specific values.

Answers: