简体   繁体   English

如何测试我的servlet是否调用了某些方法?

[英]How to test if my servlet called some method?

I have a servlet: 我有一个servlet:

class MyServlet extends HttpServlet
{
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
   {
       String one = request.getParameter("one");
       String two = request.getParameter("two");
       addNew(one, two);
   }

   public void addNew(String one, String two){
        // DAO called here to insert a new record
   }

}

I want to test if 'addNew' is called without executing its body. 我想测试是否在不执行其主体的情况下调用了“ addNew”。 I know that I need a Test Double, but don't know if a spy is the best way. 我知道我需要测试双,但不知道间谍是否是最好的方法。 I thought in doing this: 我想这样做:

class MyServletSpy extends MyServlet
{
   private boolean addNewGotCalled = false;

   @Override
   public void addNew(String one, Integer, two)
   {
       addNewGotCalled = true;
   }

   public boolean addNewWasCalled()
   {
       return this.addNewGotCalled;
   }
}

Is this the right way? 这是正确的方法吗? I feel that extending MyServlet is a litle strange, it seems that I am allowing my test to have a dependency. 我觉得扩展MyServlet有点奇怪,似乎我允许我的测试具有依赖性。

Quite hard to understand what you really want to test but you can try to take a look at Jetty if you want to test the behaviour of your Servlet : https://webtide.com/unit-test-servlets-with-jetty/ 很难理解您真正想要测试的内容,但是如果您想测试Servlet的行为,可以尝试看看Jetty: https : //webtide.com/unit-test-servlets-with-jetty/

EDIT 编辑

Thanks for your clarification. 感谢您的澄清。 In your case, one solution is to use Mockito.spy to test that the addNew method is successfully called. 在您的情况下,一种解决方案是使用Mockito.spy测试成功调用addNew方法。

First you need the mockito dependency: 首先,您需要模仿依赖:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>

Then you can use spy method to test your Servlet : 然后,您可以使用spy方法测试Servlet:

/**
 * Test of {@link MyServlet} class
 * @author rouliboy
 */
public class MyServletTest
{
   @Test
   public void testAddNewCall() throws ServletException, IOException
   {
      // Initialization
      MyServlet myServlet = new MyServlet();
      myServlet = Mockito.spy(myServlet);

      final HttpServletRequest mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
      final HttpServletResponse mockHttpServletResponse = Mockito.mock(HttpServletResponse.class);

      Mockito.when(mockHttpServletRequest.getParameter("one")).thenReturn("oneReturn");
      Mockito.when(mockHttpServletRequest.getParameter("two")).thenReturn("twoReturn");

      // Test
      myServlet.doPost(mockHttpServletRequest, mockHttpServletResponse);

      // Verification
      Mockito.verify(myServlet).addNew("oneReturn", "twoReturn");
   }
} 

EXPLANATION 说明

First we spy the Servlet using: 首先,我们使用以下方法spy Servlet:

myServlet = Mockito.spy(myServlet);

Using spy , we can check which methods are called in the Servlet. 使用spy ,我们可以检查Servlet中调用了哪些方法。

Then we mock the HttpServletRequest and HttpServletResponse inputs of doPost method. 然后,我们模拟doPost方法的HttpServletRequestHttpServletResponse输入。 Using mocks, we can define the behaviour of return values by HttpServletRequest.getParameter(...) : 使用模拟,我们可以通过HttpServletRequest.getParameter(...)定义返回值的行为:

Mockito.when(mockHttpServletRequest.getParameter("one")).thenReturn("oneReturn");
Mockito.when(mockHttpServletRequest.getParameter("two")).thenReturn("twoReturn");

And after calling doPost method, we verify that addNew is successfully called : 在调用doPost方法之后,我们验证addNew是否成功调用:

Mockito.verify(myServlet).addNew("oneReturn", "twoReturn");

However, I think that it would be better to test fully your Servlet (for example using Jetty and mocking your DAO) instead of just testing if your addNew method is called. 但是,我认为最好完全测试Servlet(例如,使用Jetty并模拟DAO),而不是仅测试是否调用了addNew方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM