简体   繁体   English

如何使用 Junit 在 servlet 中测试业务逻辑?

[英]How do I test business logic in a servlet using Junit?

I'm having a servlet that does some pre-condition checks before invoking a DAO method, like following:我有一个 servlet,它在调用 DAO 方法之前进行一些前置条件检查,如下所示:

private void processRequest(HttpServletRequest request, HttpServletResponse response){
        if(a condition is met)
            myDAOFunction();
        else
            redirect();
}

How should I construct my unit test to verify whether with a certain request, the servlet invokes my function, and with other requests that does not meet the condition then it will redirect the page?我应该如何构建我的单元测试来验证是否有某个请求, servlet调用我的 function,以及其他不满足条件的请求是否会重定向页面?

I have tried this solution: Since my DAO function would make some changes in the database if it were called, and through that I can test if the servlet handles the requests and responses correctly.我已经尝试过这个解决方案:由于我的DAO function会在数据库中进行一些更改,如果它被调用,我可以测试 servlet 是否正确处理请求和响应。 But I figure that is not quite an elegant solution.但我认为这不是一个优雅的解决方案。

So what you need to verify if the servlet can interact with the DAO related codes correctly.所以你需要验证servlet是否可以正确地与DAO相关代码交互。 If your design already separate and encapsulate all the codes related to interacting with DB in a DAO service class, you can easily test it by mocking this DAO service class using Mockito and then verify if the expected methods on the mock DAO service are invoked with the expected parameters. If your design already separate and encapsulate all the codes related to interacting with DB in a DAO service class, you can easily test it by mocking this DAO service class using Mockito and then verify if the expected methods on the mock DAO service are invoked with the预期参数。 If not, please refractor your codes such that it will have this separate DAO service class.如果没有,请重构您的代码,使其具有此单独的 DAO 服务 class。

For mocking MockHttpServletRequest and MockHttpServletResponse , spring-test already provides some utilities to create them which are useful for testing the Servlet stuff.对于 mocking MockHttpServletRequestMockHttpServletResponsespring-test已经提供了一些实用程序来创建它们,这些实用程序对于测试Servlet内容很有用。 Although they are primarily designed to work with the codes written by spring-mvc, it should also be used for the codes that are not written by spring and should be more convenient to use when compared Mockito.虽然它们主要设计用于与 spring-mvc 编写的代码一起使用,但它也应该用于不是 spring 编写的代码,并且与 Z4D1142CA816EAC3E373B383BC0B7 相比应该更方便使用。

Assuming your servlet is called FooBarServlet , the test case may look like:假设您的 servlet 称为FooBarServlet ,测试用例可能如下所示:

@ExtendWith(MockitoExtension.class)
public class FooBarServletTest { 

        @Mock
        DaoService daoService;

        @Test
        void testSaveToDatabase(){
                    
            FooBarServlet sut = new FooBarServlet(daoService);

            MockHttpServletRequest request = MockMvcRequestBuilders.get("/foobar")
                ......   
                .buildRequest(new MockServletContext());
            MockHttpServletResponse response = new MockHttpServletResponse();

            sut.processRequest(request, response);
            verify(daoService).save("xxxxxx");

        }

        @Test
        void testRedirect(){
                    
            FooBarServlet sut = new FooBarServlet(daoService);

            MockHttpServletRequest request = MockMvcRequestBuilders.get("/foobar")
                ......   
                .buildRequest(new MockServletContext());
            MockHttpServletResponse response = new MockHttpServletResponse();

            sut.processRequest(request, response);
            verify(daoService,never()).save(any());
        }

}

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

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