This is my first attempt at using EasyMock. So, I've been trying to mock a void
method:
protected void onStop() {
logger.info("Stopping ReporterService.");
if (reporter != null) {
reporter.stop();
reporter = null;
}
}
I followed a few tutorials online and I wrote this:
ReporterService reporterService1 = EasyMock.createMock(ReporterService.class);
reporterService1.onStop();
EasyMock.expectLastCall().once();
replay(reporterService1);
//EasyMock.replay(reporterService1);
assertNull(Whitebox.getInternalState(reporterService1, "reporter"));
EasyMock.verify(reporterService1);
But then I got this:
java.lang.AssertionError:
Expectation failure on verify:
ReporterService.onStop(): expected: 1, actual: 0
I searched online but I have no idea why it says that. Appreciate the help.
Also, just to for the sake of understanding, I tested this manually and it works, I just want to test it using Easymock:
ReporterService reporterService = new ReporterService();
Reporter reporter = new Reporter(null,null,null);
Whitebox.setInternalState(reporterService , "reporter", reporter);
assertNotNull(Whitebox.getInternalState(reporterService, "reporter"));
reporterService.onStop();
assertNull(Whitebox.getInternalState(reporterService, "reporter"));