Java Servlet unit testing

Java Servlet unit testing

1. solve pain points

Although the mainstream of development methods, many of which are provided by controll or micro-api service, but can not help but still need to write several servletcomplete interface development. According to the routine, servletcall the service layer code, just do the next service layer unit test just fine. But here ignores the request parameter testing process, in accordance with the past, if you need to test, often the first to run a tomcat, and then find an excuse to test tools, complete interface testing. in fact, no such trouble, use spring-testto quickly get solve the the following pain points

  • Unit test coverage is not comprehensive
  • Do not start a servlet container
  • The interface does not require third-party testing tools

2. implementation

Since it is doing unit testing, we first need to introduce in the pom file junitand spring-testtwo dependencies

<!--author:herbert wx:464884492-->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${springVersion}</version>
  <scope>test</scope>
</dependency>

Then declare the unit test code, we need to do unit testing servletobjects instantiate this object class loading unit testing.

// author:herbert wx:464884492
public class DemoServletTest {
    DemoServlet servlet = null;

	@Before
	public void setUp() {
		servlet = new DemoServlet();
		DOMConfigurator.configure("src/main/webapp/WEB-INF/log4j.xml");
	}
}

We then add the specific test method, a few simple code request completes a package.

// author:herbert wx:464884492
@Test
public void testReqImg() throws ServletException, IOException, InterruptedException {
  JSONObject p = new JSONObject();
  p.put("demoRequest", "herbert 通过spring-test 测试servlet");
  MockHttpServletRequest request = new MockHttpServletRequest();
  request.setContent(p.toString().getBytes());
  MockHttpServletResponse response = new MockHttpServletResponse();
  servlet.doPost(request, response);
  while (true) {
  	Thread.sleep(200);
  }
}

It is the realization of the principle MockHttpServletRequestand MockHttpServletResponserespectively implements the interface HttpServletRequestand HttpServletResponseunder normal circumstances, the container automatically, so generally do not pay attention to the specific implementation of the contents of the development. If you are interested, can achieve their own these two interfaces, complete unit tests.

3. Summary

Knowledge is small, focusing on the accumulation of .2020 is bound to be an extraordinary year. Come on !!

Welcome interested friends concerned about my subscription number, "a small hospital is not small," Fanger Wei code or concern clicks. I will be difficulties encountered in development for many years, as well as some interesting features, the experience will be 11 to publish my subscription number in
Subscription number

Guess you like

Origin www.cnblogs.com/yfrs/p/servletTest.html