I.Enviroment:

Class UserDao {
   User findUser(int id) {...}
   void saveUser(User user) {...}
}
Class Authentication{
    boolean isHaveAccess(){}
}
Class UserManager{
   void updateUser(int id) {
   if(!objectAuthentication.isHaveAccess())
       thow AuthenticationException ;
   validateUserInfo();
   User user = objectUserDao.findUser(id);
   objectUserDao.saveUser(user);
   }
   boolean validateUserInfo() {...}

}

II. Write a unit test to test createUser :

– In createUser method, you see that have two object (objectAuthentication and objectUserDao).But in this unit test, you just focused test behaviour createUser.

Don’t care about test for behaviour authentication and save user.Because you will have another unit test to test these saveUser and check Authentication.So you will create 2 object Authentication and UserDao are a mock object.

1.Create Mock object with EasyMock framework:

– There are 3 type mock Object:

+ Normal – EasyMock.createMock() : All of the expected methods must be called with the specified arguments. However, the order in which these methods are called does not matter. Calls to unexpected methods cause the test to fail.

+ Strick – EasyMock.createStrickMock(): All expected methods must be called with the expected arguments, in a specified order. Calls to unexpected methods cause the test to fail.

+ Nice – EasyMock.createNiceMock(): All expected methods must be called with the specified arguments in any order. Calls to unexpected methods do not cause the test to fail. Nice mocks supply reasonable defaults for methods you don’t explicitly mock. Methods that return numbers return 0. Methods that return booleans return false. Methods that return objects return null.

2. Expecting mock object method calls using EasyMock.expect :

3. Replaying expected method using EasyMock.replay():

If you forget to call EasyMock.replay() before using a mock, you’ll get an IllegalStateException with the not especially helpful error message: missing behavior definition for the preceding method call.

4. Verifying the expected method using EasyMock.verify():

This is the last step create unit test. It will validate all method you expected.

*Finish test:

Class UserManagerTest {
    UserDao mockUserDao;
    Authentication mockAuthentication;
    @BeforeClass
    void init() {
        mockUserDao = EasyMock.createMock(UserDao.class);
        mockAuthentication = EasyMock.createMock(Authentication.class);
   }
   @test
   public void testUpdateUser() {
       //init User
       User user = new user;
       user.setName("test");
       user.setPassword("pass");
       //init isAccess
       boolean isHaveAccess = true;
       //expect
       EasyMock.expect(mockAuthentication.isHaveAccess()).andReturn(isHaveAccess);
       EasyMock.expect(mockUserDao.findUser(1)).andReturn(user);
       EasyMock.expect(mockUserDao.saveUser());
       //replay
       EasyMock.replay(mockUserDao);
       EasyMock.replay(mockAuthentication);
       //call test method
       //if method have return .you should use Assert
       userManager.updateUser(1);
       //call verify
       EasyMock.verify(mockUserDao,mockAuthentication);
   }
}

If anyone read it. Please suggest to me if have anything wrong or need to improve.

Advertisements