1. Create a unit test with EasyMock:

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);
   }
}

–> Change to use with

Class UserManagerTest {
    @Mock
    UserDao mockUserDao;
    @Mock
    Authentication mockAuthentication;
    @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
       EasyMockUnitils.replay();
       //call test method
       //if method have return .you should use Assert
       userManager.updateUser(1);
   }
}

		
Advertisements