????????????????
???????????? ???????[ 2012/7/19 10:55:57 ] ????????
????????????????????????????
?????? ??д????
?????? ???д????γ???????
?????? ???????????????????
?????? ??????Щ?????ò??????е???м???????????
?????? ???????????????????????
?????? ???mock????
??????д????
????????????????????????????????????Ч???????д????????????ò?????????????????ó???????????????????????????в??????????????????????????μ????????????д????????????????????????????????μ????????????д????????????
?????????????????????д?????????????????????????????????????????д???д???????д???????????????????С?????ò?????????????Щ???????????д???д????????д?Щ????????????????????????
??????????д??????ζ?????????????????????????????????????????????????????????????????д?κδ???????д?κβ??????????
???????д????γ???????
????д??????????????????????????????????????α??д???????????????????????κζ????????????????????????????????????????????????????????????????д????????????????е????????????????д??????????????е??
???????????????????????
??????????????????????????????????????????????????е??????????????????Щ??????????????????????????????÷???????null?????????????????????????????飿???????????????????????????????????????????????????????Щ??????????????????
???????????????д?????????????????д?????????????????????????????????
??????????Щ?????ò??????е???м???????????
??????????????????д?????2????
public void testFindUsersByEmailNoMatch() {
assertEquals(
"nothing returned"??
0??
new UserRegistry().findUsersByEmail("not@in.database").length);
}
?????????????????????????????С??????????£?
public User[] findUsersByEmail(String address) {
return new User[0];
}
????д??????????ò??????????????????????????“???????????”????????????????д????????????????????????????????????????????????????????????
????????????????????????????????????ò????????????????????????????????????д??testFindUsersByEmailOneMatch??testFindUsersByEmailMultipleMatches???????????????????????ЩС???????и?????????????????????????null??
??????????????????????????????????????????????????????????????????Щ??????????
???????????????????????????
???????????????????????????ò??????????÷??????????????????????????????????????????Ρ????磬?????????????????????????????????????????????????????????????????????????????????????????β?????????????“???”??????????????????????????????????????????????????????
?????????????????????????????д??ó???????????????????“???”??????μ????μ?mock???????????????????????“????”????????????????д?????????????????????????????????д??????????????Щ????????????
?????????????????????????????????????????????????????????????????????Χ???????????????????????????????????????????????EventListener????????/???????????????????ò??????????????????????????????
???????mock????
????mock????????п?????????????????????????????????????μ?????????????????????????????????????java.lang.reflect.Proxy??д??mock??????????????????????????????????Щ??????????
????mock????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????“removeUser” ????????????“cmiller”??
????mock??????????Ч???????????EJB??????????2????EJB ??????????????????????????EJB?????????????????????????????????????????????δ????????????EJB——??????????????????????????л??????????????????
import org.pastiche.devilfish.test.*;
import junit.framework.*;
import java.lang.reflect.*;
import javax.ejb.SessionContext;
public class MonkeyPlaceTest extends TestCase {
private static class FailingMonkeyFactoryHandler
implements InvocationHandler {
public Object invoke(Object proxy?? Method method?? Object[] args)
throws Exception {
if (method.getName().equals("createMonkey"))
throw new MonkeyFailureException("Could not create");
throw new UnsupportedOperationException("Didn't expect that");
}
}
public void testFailedCreate() throws Exception {
MockObjectBuilder factoryBuilder = MockObjectBuilder.getInstance(
MonkeyFactory.class??
new FailingMonkeyFactoryHandler());
MockObjectBuilder contextBuilder = MockObjectBuilder.getInstance(
SessionContext.class);
MonkeyPlaceBean bean = new MonkeyPlaceBean();
bean.setSessionContext(
(SessionContext)contextBuilder.getObject());
try {
bean.newMonkey(
(MonkeyFactory)factoryBuilder.getObject()??
"fred"??
"bananas");
fail("Expected monkey failure exception");
} catch (MonkeyFailureException e) {
assertEquals("session was rolled back"??
new MethodCall("setRollbackOnly")??
contextBuilder.getHandler().getMethodCall(0).getName();
}
}
}
??????
???·???
??????????????????
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