3. Unit test in java
● Main framework : Junit and TestNG
● Database management (fill or check content)
– DbUnit
– DbSetup
● Mock object frameworks
– EasyMock
– Mockito
4. Goal of EasyMock
● On the web site of EasyMock (www.easymock.org)
– Great (unit) testing includes isolation
– Isolation involves mock objects
– EasyMock makes mocking easier
6. Mock objects
● Unit tests are white box tests
– The developers is allowed to look at the code
● Mock objects replaces the objects linked to the object we
want to test
● It becomes possible to test more path (exceptions, errors)
● We have to change a little the implementation to make the
mocking easier
7. Mock objects
● Unit tests are white box tests
– The developers is allowed to look at the code
● Mock objects replaces the objects linked to the object we
want to test
● It becomes possible to test more path (exceptions, errors)
● We have to change a little the implementation to make the
mocking easier
8. Features of Mock frameworks
● Main features
– Value returning for a stubbed method
– Exception throwing for a stubbed method
– Invocation check method
– Method arguments check
– Invocation order for one mock check
● Advance features
– Stubbing of equals() and hashCode() methods
– ...
9. Features of Mock frameworks
● Main features
– Value returning for a stubbed method
– Exception throwing for a stubbed method
– Invocation check method
– Method arguments check
– Invocation order for one mock check
● Advance features
– Stubbing of equals() and hashCode() methods
– ...
10. Main mock frameworks (in java)
● Open projects well know
– EasyMock
– Jmockit
– Mockito
– Jmock
11. EasyMock live cycle of mock
objects
● Create a mock
● Set up your expectation
● Set the mock to replay mode
– Call your code under test
● Verify that your expectation have been set
12. EasyMock create mock (strict of
nice)
● Create a mock
– CreateNiceMock()
● Unexpected returns an empty value (0 or null)
– CreateMock()
● The order of the call is not checked
– CreateStrictMock()
● The order of the call is checked
15. EasyMock How to inject the mock
@RunWith(EasyMockRunner.class)
public class StudentManagerMockTest {
@TestSubject //where to inject mocks
private static StudentManager studentManager;
@Mock(type = MockType.NICE) //mock to inject
private StudentQuery studentQueryMock =
EasyMock.createNiceMock(StudentQuery.class);
.../...