2013-03-23 101 views
1

我有一个测试Servlet的问题。保镖是一个简单的方法doPost和init受我重写的Servlet。但是,当我运行代码,我得到异常Junit和EasyMock Servlet测试

@Before 
     public void Before() throws IOException, ServletException, 
       InstantiationException, IllegalAccessException, 
       IllegalArgumentException, InvocationTargetException, 
       NoSuchMethodException, SecurityException, ClassNotFoundException { 
      encoder = EasyMock.createMock(Encoder.class); 
      EasyMock.expect(encoder.encode("password")).andReturn("asdf"); 
      EasyMock.expect(encoder.encode("nic")).andReturn("asss"); 
      EasyMock.expect(encoder.encode("Password")).andReturn("ass"); 
      EasyMock.replay(encoder); 
      db = EasyMock.createMock(UserDataBase.class); 
      db.connect(); 
      EasyMock.expect(db.isConnected()).andReturn(true); 
      EasyMock.expect(db.getUserByLoginAndPassword("login", "asss")) 
        .andReturn(null); 
      EasyMock.expect(db.getUserByLoginAndPassword("login", "asdf")) 
        .andReturn(new User("Rafal", "Machnik")); 
      EasyMock.expect(db.getUserByLoginAndPassword("fake", "asdf")) 
        .andReturn(null); 
      EasyMock.expect(db.getUserByLoginAndPassword("login", "ass")) 
        .andReturn(null); 
      EasyMock.replay(db); 

      lsf = EasyMock.createMock(LoginServiceFactory.class); 
      EasyMock.expect(lsf.getEncoder()).andReturn(encoder).anyTimes(); 
      EasyMock.expect(lsf.getUserDataBase()).andReturn(db).anyTimes(); 
      EasyMock.replay(lsf); 

      config = EasyMock.createMock(ServletConfig.class); 
      EasyMock.expect(config.getInitParameter("LoginServiceFactory")) 
        .andReturn("pl.to.cw4.LoginServiceFactory"); 
      EasyMock.replay(config); 

      request = EasyMock.createMock(HttpServletRequest.class); 
      EasyMock.expect(request.getParameter("login")).andReturn("login") 
        .anyTimes(); 
      EasyMock.expect(request.getParameter("password")).andReturn("password") 
        .anyTimes(); 
      EasyMock.replay(request); 

      pageSource = new StringWriter(); 

      response = EasyMock.createMock(HttpServletResponse.class); 
      EasyMock.expect(response.getWriter()) 
        .andReturn(new PrintWriter(pageSource)).anyTimes(); 
      EasyMock.replay(response); 

      bouncer = new Bouncer(lsf); 

      bouncer.init(config); 

     } 

     @Test 
     public void bouncerTest() throws ServletException, IOException { 
      bouncer.service(request, response); 
      assertNotNull(pageSource.toString()); 

     } 

java.lang.AssertionError: 意外的方法调用getMethod(): 在org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:32)..

如果有人知道如何解决它,我会很感激。

回答

1

该错误指示easymock在模拟对象中遇到方法调用getMethod()。逐行调试程序并为模拟对象添加期望调用。

如果不是模拟对象,则不需要添加方法调用,但应将模拟对象中的所有调用添加到您的测试方法中。

getMethod()被调用的服务,因为你是嘲笑HttpServletRequest的,你还需要模拟所有的方法调用上的HttpServletRequest

+0

好的;)谢谢我一定记得那个。 – RMachnik 2013-03-23 22:19:19

1

service()方法调用getMethod()来确定是否必须调用doGet()doPost()或其他servlet方法。由于您没有根据您的模拟请求将此调用存根getMethod(),EasyMock会抛出此异常。

为什么不直接致电doPost(),而不是致电service(),因为这是您想要测试的方法?

+0

我没有直接调用的doPost(),因为它在默认情况下保护和我的测试) – RMachnik 2013-03-23 22:16:12

+0

好的,现在可以,我把它改为公开,所以我可以直接做到这一点,谢谢你的帮助;) – RMachnik 2013-03-23 22:18:14

+0

这是一个传统的做法,把测试与被测试的类放在同一个包中。你也可以公开这个方法。如果你不想这样做,那么你必须存储由service方法在内部调用的所有方法。 – 2013-03-23 22:18:17