broken unit test with easyMock

there is a need to add in further logging, to assist the developer for troubleshooting an API call.

however, with such a simple logging statement, it broke the pipeline because a bug in the implementation with easyMock.

the new log line:

if the response is not null, log the status and header. otherwise log as null.

above small code changes however broke the CI/CD. after some investigation, it is found the original developer who coded the unit test, which supposed to protect the application, would break if the status method is not called 2 (a magic number) times.

The fix is, it really doesn’t need to hard code the times the method is called, instead it should use andStubReturn instead of andReturn method.

the fix

the problem with easyMock is (beside the clear no-go with replay/replayAll), the default andReturn method expect times of the call, which default is 1. the developer when implement the unit test at the time, has called the getStatus method 2 times, hence hardCode it with 2. this will break if any developer or use case, that the method needs to be called 1 or any times more.

While the correct method for easyMock to use is `andStubReturn

Leave a comment