????UserDao.validate(String)?????????????????ж??????????“admin”??????true????????false???????????????????????

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class UserDaoTest {
private static final String USERNAME = "liang";
private UserDao userDao = new UserDao();
private ServiceClient client;
private List<String> groupContainsAdmin;
private List<String> groupNotContainAdmin;
@Before
public void before() {
client = createMock(ServiceClient.class);
groupContainsAdmin = new ArrayList<String>() {
{
add("admin");
add("user");
}
};
groupNotContainAdmin = new ArrayList<String>() {
{
add("user");
}
};
}
@Test
public void testValidateIsAdmin() {
expect(client.getGroupsByUser(eq(USERNAME))).andReturn(
groupContainsAdmin);
userDao.setClient(client);
replay(client);
boolean isAdmin = userDao.validate(USERNAME);
assertTrue(isAdmin);
verify(client);
}
@Test
public void testValidateIsNotAdmin() {
expect(client.getGroupsByUser(eq(USERNAME))).andReturn(
groupNotContainAdmin);
userDao.setClient(client);
replay(client);
boolean isAdmin = userDao.validate(USERNAME);
assertFalse(isAdmin);
verify(client);
}
}

???????????????????????£?

????1.????????????????????EasyMock.createMock(Class)??????????ServiceClient??Mock????

????2.??????????У????EasyMock.expect()???????Mock????????????testValidateIsAdmin()??????getGroupsByUser()???????????????USERNAME????????????groupContainsAdmin????testValidateIsNotAdmin()??getGroupsByUser()????groupNotContainAdmin???????????????EasyMock.eq()??????andReturn()??????

????3.Mock??????????????????EasyMock.replay()??????Mock????????????

????4.????????????????????????????????????

????5.?????EasyMock.verify()?????????????????????????????????????????????????????е???????

????EasyMock????????????????????EasyMock.isA(Class)??EasyMock.andStubReturn()?????????????????????????ο?http??//easymock.org/EasyMock3_1_Documentation.html?????????????????????

????????EasyMock??в???????????????EasyMock??????Java????????????????????????????????Mock?????????и??????PowerMock???cglib??????????????????????????????Mock???????????????PowerMock??????EasyMock???????????EasyMock???????????