| springmvctest Summary: Recently to do unit testing, so the choice is springtest this test framework. 1. Preparatory work. (Import jar package) because using MAVEN to manage jar packages, add the following code to the Pom file in the module where you want to do the unit test: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${ spring.version}</version> </dependency> < dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> & nbsp <version>${spring.version}</version> </dependency> & nbsp <dependency> <groupId>org.springframework</groupId>   <artifactId>spring-jdbc</artifactId> < version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> & nbsp <dependency> <groupid>org.springframework</ groupid> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </ dependency> <dependency> &nbsP <groupId>org.aspectj</groupId> < artifactid>aspectjweaver</artifactid> <version>1.8.5</ version> </dependency><dependency> & lt;groupid>junit</groupid> <artifactid>junit</artifactid > <version>4.12</version> &NBSP ; <scope>provided</scope> </dependency> < dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> & nbsp <version>${spring.version}</version> &nbsP </dependency> Of course you can write the above code in the parent module, so you don't have to write the code repeatedly in each submodule. 2. Write the Test class. @RunWith This is a class annotation that shows what the execution class of the test class is. If not, the test execution class will be the default. Common Test Execution Class: (1) springjunit4classrunner.class: (2) Parameterized.class: Parameterized Runner with @ Parameters uses JUnit's parameterization feature (3) JUNIT4.CLASS:JUNIT4 's default runner (4) Suite.class, using the following: @ Runwith (Suite.class) @SuiteClasses ({atest.class,btest.class,ctest.class}) When performing the test class with the above notation, multiple test classes, such as Atest.class,btest.class,ctest.class, will also execute. @ContextConfiguration Annotations, this is a class-level annotation. The note has the following two common properties: locations: This property allows you to manually specify the location of the spring configuration file, and you can specify one or more spring configuration files. As shown below: @ContextConfiguration (locations={"Xx/yy/beans1.xml", "xx/ Yy/beans2.xml "}). If this property is not set, The configuration file is found in the default location, the default profile is "test class name-context.xml" Inheritlocations: If you want to inherit the Spring configuration file from the parent test case class, the default is true. 3. Run the test. Because I'm building a maven project, I'm going to putThe test class for each module is placed in the Src/test directory of the corresponding module, so when I execute "mvn clean test", maven automatically executes the test cases in all the test classes in the Src/test directory. is very convenient, do not click the test case individually. problems: has not studied the rest service testing, and the DAO layer, service layer. So we need to continue to study. |