??EasyMock???е???????????EasyMoc
???????????? ???????[ 2013/7/11 14:13:17 ] ????????
????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???????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11