Spring boot can work with most popular test frameworks: Create unit Tests from spring JUnit, generate test data initialization database for testing, spring boot can be combined with BDD (Behavier Driven Development) tool , Cucumber, and Spock work together to test the application.
When it comes to software development, we write a lot of code, but in another six months (or even more than a year) do you know how your code works? Through testing (unit test, Integration test, interface test) can guarantee the maintainability of the system, when we modify some code, through the regression test can check whether to introduce a new bug. In a sense, the test makes the system no longer a black box, allowing the developer to confirm that the system is available.
In a Web application, there are generally two ways to test a controller layer: (1) Send an HTTP request, or (2) impersonate an HTTP request object. The first method needs to configure the regression environment to calculate coverage by modifying the policies of the Code statistics, the second method is more formal thinking, but in my current experience of the project is not used much, today summarizes how to use mock Object Test controller layer code.
In previous articles, we used the bookpub application as an example, and today is no exception, ready to test whether the RESTful interface it provides returns the correct response data. This test is different from unit testing , where the full application context needs to be initialized, all spring beans are woven, and test data is required in the database, which is generally referred to as integration testing or interface testing .
MVC Test Code:
Package Com.letv.bigdata.idfa.test.controller;import Com.letv.bigdata.idfa.controller.idfacontroller;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.test.autoconfigure.web.servlet.autoconfiguremockmvc;import Org.springframework.boot.test.autoconfigure.web.servlet.webmvctest;import Org.springframework.boot.test.context.springboottest;import Org.springframework.boot.test.mock.mockito.MockBean ; import Org.springframework.test.context.junit4.springrunner;import org.junit.*; import Org.junit.runner.*; Import Org.springframework.test.context.web.webappconfiguration;import Org.springframework.test.web.servlet.mockmvc;import Org.springframework.test.web.servlet.request.mockmvcrequestbuilders;import Org.springframework.test.web.servlet.result.mockmvcresultmatchers;import Org.springframework.test.web.servlet.setup.mockmvcbuilders;import Org.springframework.web.context.WebApplicationContext; the @RunWith (Springrunner.class) @SpringBootTest @autoconfiguremockmvc@webappconfiguration Public classIdfacontrollertest {PrivateMOCKMVC MVC; @AutowiredPrivateWebapplicationcontext context; @Test Public voidtest () throws Exception {Mvc.perform (mockmvcrequestbuilders.Get("/query") . Andexpect (Mockmvcresultmatchers.status (). IsOk ());//. Andexpect (Mockmvcresultmatchers.content (). String ("abc"));} @Before Public voidSetupmockmvc () throws Exception {MVC=Mockmvcbuilders.webappcontextsetup (context). build (); }}
Simple get test can be done, other pending
springboot-32-using MVC Testing