Java Reference
In-Depth Information
@Before
public void
setFixtures() {
facade =
new
UserFacadeImpl();
TestSetup.setup(this);
facade.setUserDao(dao);
}
D
E
@Test
public void
testGetUserById() {
int
id = 666;
User user = newUserWithTelephones();
expect(dao.getUserById(id)).andReturn(user);
replay(dao);
UserDto dto = facade.getUserById(id);
assertUser(dto);
verify(dao);
}
}
As you can see on
B
, Mycila doesn't require any special inheritance or custom run-
ner, which forces the test case to explicitly call its
TestSetup.setup()
in a
@Before
method
D
. That method then scans the test class looking for
@Mock
annotations and
does the proper EasyMock setup when they're found, such as in
C
. Notice that each
test case statement that requires a reference to these mocks (like the dependency
injection defined in
E
) must be executed after
TestSetup.setup()
is called. Mock
injection is the only mock support Mycila provides; the test method itself
F
is respon-
sible for calling
replay()
and
verify()
in the mocks.
Mycila also supports other mock frameworks, such as
JM
ock (also analyzed in chap-
ter 6) and Mockito (
http://mockito.org
). Listing 19.4 shows the same example using
the
JM
ock plug-in.
F
Listing 19.4
UserFacadeImpl
test refactored to use Mycila JMock plug-in
[...]
import static
com.manning.junitbook.ch19.model.EntitiesHelper.*;
import
org.jmock.Expectations;
import
org.jmock.Mockery;
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.jmock.Mock;
import
com.mycila.testing.plugin.jmock.MockContext;
B
public class
UserFacadeImplMycilaJMockTest {
private
UserFacadeImpl facade;
C
@MockContext
private
Mockery context;