????????????????????????????

?????? ??д????

?????? ???д????γ???????

?????? ???????????????????

?????? ??????Щ?????ò??????е???м???????????

?????? ???????????????????????

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