JUnit


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

	
	

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.

From “JUnit in Action”.

1. Logic unit test:

– A test that exercises the code logic by focusing on a single method. You can control the boundaries of given test method using mock objects or stubs.

2. Integration unit test:

– A test that focuses on the integration between components in their real environment ( or part of the real environment ). For example , code that accesses a database has tests that effectively call the database.

3. Functional unit test:

– A test that extend the boundaries of integration unit testing to confirm a stimulus-response. For example, a web application contains a secure web page that only authorized client can access. If the client does not log in, then trying to access the page should result in a redirect to the login page. A functional unit test can examine this case by sending an HTTP request to the page verify the redirect ( HTTP status code 302) response code come back.