I have built a mock object using EasyMock, and I'm trying to have the object return another object after a method call. The method call does not cause any exception to be thrown, but it returns null, though I am certain the andReturn()
call had a non-null argument.
The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called. The replay() method is called to make the Mock object available. // setup the mock object expect(calcMethod.
EasyMock is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. EasyMock is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.
The replay method is used to pass the mock from recording (where you record the method you expect to be called) to replaying state (where you actually test).
The problem here was that I had not called replay()
on the mock object after specifying the behavior. For some reason, this caused it to allow some of the expected behaviors, but always return null for when a return value was expected. Remember to call replay()
before using the mock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With