Afraid to forget later, casually write
1. Spring configuration file
The class that is declared as a bean declares that the project is to be run before the test, so the spring's configuration file problem is not much said, and the following database configuration is related to the transactionalconfigration annotation responsible for rollback in the test class, so post it.
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "Mysqldatasource"/>
</bean>
2, Pom.xml need to use the package:
<!--TEST--> <dependency> <groupId>junit</groupId> <artifactid>junit</artifact id> <version>4.10</version> <scope>test</scope> <exclusions> <EXCLUSION&G
T <artifactId>hamcrest-core</artifactId> <groupId>org.hamcrest</groupId> </exclusion>
; </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> & Lt;artifactid>spring-test</artifactid> <version>3.2.8.RELEASE</version> <scope>test </scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifacti d>hamcrest-all</artifactid> <version>1.3</version> <scope>test</scope> </depe ndency> <dependency> <groupId>org.mockito</groupId> <artifactid>mockito-core</artif Actid> <version>1.9.5</version> <scope>test</scope> <exclusions> <exclusion> <arti Factid>hamcrest-core</artifactid> <groupId>org.hamcrest</groupId> </exclusion> < /exclusions> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <a rtifactid>json-path</artifactid> <version>0.8.1</version> <scope>test</scope> &L T;/dependency>
3. Test class
It is best to create a new test folder and package in the source parallel directory
@RunWith (Springjunit4classrunner.class) @TransactionConfiguration (TransactionManager = "TransactionManager"), Defaultrollback = True)//ensures that the database is rolled back each time the test class is executed, preventing dirty data from being generated @Transactional @WebAppConfiguration (value = "mydemo/src/main/ WebApp ") @ContextConfiguration (locations={" File:src/main/webapp/web-inf/spring-config.xml "})// If the spring configuration file is placed in the Web-inf directory, you need to use this method @ActiveProfiles ("Dev") public class Demotest {@Autowired Webapplicationcontex
T WAC;
Private MOCKMVC Mockmvc;
@Before public void SetUp () {MOCKMVC = Mockmvcbuilders.webappcontextsetup (WAC). build (); @Test public void GetDemoidTest1 () throws Exception {Mockmvc.perform mockmvcrequestbuilders.get ("/config/ Getdemo?demoid=1005 ")). Anddo (Mockmvcresulthandlers.print ()). Andexpect (Mockmvcresultmatch Ers.status (). IsOk ()). Andexpect (Mockmvcresultmatchers.content (). ContentType ("application/json;charset=utf- 8 ")). Andexpect (Jsonpath (" $).Code "). Value (20000)). Andexpect (Jsonpath (" $.demolist "). Exists ()). Anddo (Mockmvcresulthandl
Ers.print ()). Andreturn (); }
}
1, Mockmvc.perform to execute a request;
2. Mockmvcrequestbuilders.get ("/USER/1") constructs a request
3, Resultactions.andexpect add execution after completion of the assertion
4. Resultactions.anddo Adds a result processor that indicates what to do with the result, such as using Mockmvcresulthandlers.print () to output the entire response result information.
5. Resultactions.andreturn indicates that the corresponding results are returned after the execution is completed.
4, which may encounter the pit:
1, if spring's configuration file is in the default directory, use @contextconfiguration (locations = "Classpath:spring-config.xml")
But. If spring's configuration file is in the Web-inf directory, use the
@ContextConfiguration (locations={"File:src/main/webapp/web-inf/spring-config.xml"})
Otherwise, the configuration file will not be found
Speaking here can mention why the need to load the configuration file, using MOCKMVC, will create a complete set of processes, simulation from the front of the request, the individual understand to use this method to invoke controller and front-end, postman and other calls controller no difference, This allows you to test the complete spring MVC process, from URL requests to controller processing, to view rendering.
2), executive use case times this anomaly java.lang.NoSuchMethodError:com.jayway.jsonpath.JsonPath.compile (ljava/lang/string;[ Lcom/jayway/jsonpath/filter;) Lcom/jayway/jsonpath/jsonpath;
Solution: This abnormal egg pain, I feel all normal, but the use case is reported abnormal, and then checked, found this solution < dependency > < groupId > Com.jayway.jsonpath </ groupId > < artifactid > Json-path </ artifactid > &L T version > 0.8.1 </ version > < scope > Test </ scope > </ Dependency >
Try to reduce the version of the Json-path, or reload the dependencies of Maven
I have just come in contact with the wrong place and I ask Daniel to point out