Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock andReturn() null return value

Tags:

null

easymock

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.

like image 816
Zach Avatar asked Jun 18 '12 21:06

Zach


People also ask

What is andReturn()?

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.

What does EasyMock do?

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.

What is EasyMock replay?

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).


1 Answers

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.

like image 69
Zach Avatar answered Sep 28 '22 15:09

Zach