Java Reference
In-Depth Information
If you forget to include a module, Unitils won't instantiate the attributes that use
annotations from that module, and the test case will eventually throw an excep-
tion. For instance, if the
easymock
module was not included, the
dao
reference
wouldn't be set, and the
expect(dao.getUserById(id))
statement would throw a
NullPointerException
.
Besides configuring which modules are used, you can also change some module
behavior through module-specific properties. For instance, to disable calls to Easy-
Mock's
verify()
after the test cases are run, you set
EasyMockModule.autoVerify-
AfterTest.enabled
to
false
. It's also possible to set the mock behavior mode
(lenient or strict), even if the order of calls should be taken into account.
Another interesting Unitils feature is the
@Dummy
concept. Attributes annotated
with
@Dummy
behave similarly to those annotated with
@Mock
, except that you don't
need to set expectations: all method calls will return default values (such as
0
for
methods that return an int). These dummies are convenient for cases where your
tested objects need a valid reference to another object, but the behavior of that object
is irrelevant for the test case where it's used.
19.2.2
FEST-Mocks
Listing 19.2 shows the same example using
FEST
-Mocks.
Listing 19.2
UserFacadeImpl
test refactored to use FEST-Mocks
[...]
import static
org.easymock.EasyMock.*;
import
org.fest.mocks.EasyMockTemplate;
import
org.junit.Before;
import
org.junit.Test;
import
com.manning.junitbook.ch19.dao.UserDao;
import
com.manning.junitbook.ch19.model.User;
import
com.manning.junitbook.ch19.model.UserDto;
B
public class
UserFacadeImplFESTTest {
private
UserFacadeImpl facade;
private
UserDao dao;
@Before
public void
setFixtures() {
facade = new UserFacadeImpl();
dao = createMock(UserDao.
class
);
facade.setUserDao(dao);
}
C
@Test
public void
testGetUserById() {
final int
id = 666;
final
User user = newUserWithTelephones();
new
EasyMockTemplate(dao) {
D