Java Reference
In-Depth Information
protected void
expectations()
throws
Throwable {
expect(dao.getUserById(id)).andReturn(user);
}
@Override
protected void
codeToTest()
throws
Throwable {
UserDto dto = facade.getUserById(id);
assertUser(dto);
}
}.run();
}
FEST
-Mocks doesn't require the test class
B
to extend any class or to use any special
runner; as a drawback, it's necessary to manually instantiate the mocks and objects
being tested
C
. All it does is provide an abstract template class that must be extended
on each test case
D
, which in turn must explicitly set the expectations and run the
code to be tested. Then when the method
run()
is called
E
, it executes a workflow
similar to that described at the beginning of this section (the main difference is that
the verify step isn't optional, and
verify()
is always called).
Overall,
FEST
-Mocks is a bit convoluted, because it explicitly uses the Template
Design Pattern, but in a complex way. Its creators claim that separating the mock's
expectation and code being tested makes the test case clear. Although we agree that
the result is clear to read, it seems less natural and more verbose to develop.
E
19.2.3
Mycila
Mycila mock support is similar to Unitils in the way that you mark your mock attri-
butes with annotations. Unlike Unitils, however, you still need to do some manual
setup in a
@Before
method, such as creating the objects being tested and calling the
Mycila initialization method. Listing 19.3 shows our example converted to Mycila.
Listing 19.3
UserFacadeImpl
test refactored to use Mycila EasyMock plug-in
[...]
import static
com.manning.junitbook.ch19.model.EntitiesHelper.*;
import static
org.easymock.EasyMock.*;
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;
import
com.mycila.testing.core.TestSetup;
import
com.mycila.testing.plugin.easymock.Mock;
B
public class
UserFacadeImplMycilaEasyMockTest {
private
UserFacadeImpl facade;
@Mock
private
UserDao dao;
C