2013-03-23 96 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