The @runwith annotation for the Springboot test class
@runwith
- @runwith is a runner
- @runwith (junit4.class) means running with JUnit4
- @ RunWith (SpringJUnit4ClassRunner. Class), make a test run in
A Spring test environment that automatically creates the Spring application context –@RunWith(suite.class) at the start of a test is a set of tests
Spring Boot 1.5.2 Junit tests use Spring for unit testing
Method 1: [Reference]
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class BBTestAA {
@Autowired
private TestRestTemplate testRestTemplate;
//Application.class is the SpringBoot entry class, which is configured for each SpringBoot project
}
Copy the code
If the pom. There are the following code in the XML, this line of @ RunWith (SpringRunner. Class) will not appear SpringRunner, instead have @ RunWith (SpringJUnit4ClassRunner. Class)
<!--spring-test测试=-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.24..release </version> </dependency> @runwith (springrunner.class) will not report an error if this is not present in pom.xml. <scope>test</scope> </scope> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> An error will be reported if this paragraph is not present in pom.xml. <scope>test</scope>SpringRunner, SpringBootTest cannot be referenced, will report error; <scope>test</scope> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.59..RELEASE</version>
<scope>test</scope>
</dependency>
Copy the code
To sum up, want to use
@runwith (springrunner.class) @Springboottest (classes = app.class) Pom.xml should reference both of these
<! --spring-test test =--> <! --<dependency>--> <! --<groupId>org.springframework</groupId>--> <! --<artifactId>spring-test</artifactId>--> <! --<version>4.24..RELEASE</version>--> <! --</dependency>--> <! -- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.59..RELEASE</version> <! --<scope>test</scope>--> </dependency> <! -- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version> <! --<scope>test</scope>--> </dependency>Copy the code
Method 2: [Reference]
If test@RunWith is reported red, no test will be introduced
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Copy the code
If test@SpringBootTest is reported red, no test will be introduced
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.59..RELEASE</version>
<scope>test</scope>
</dependency>
Copy the code
If it is 4.2.4 RELEASESpringRunner red, if 4.2.4. RELEASE will introduce the class
<!--spring-test测试=-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.37..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.59..RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Copy the code
2. Add JunitTest class to IDE
@RunWith(SpringRunner.class) / / 14. Version before using SpringJUnit4ClassRunner. Class
@SpringBootTest(classes = Application.class) / / version 1.4 before use is / / @ SpringApplicationConfiguration (classes = Application. The class)
public class SystemInfoServiceImplTest {
@Autowired
private ISystemInfoService systemInfoservice;
@Test
public void add(a) throws Exception {} @Test
public void findAll(a) throws Exception {}}Copy the code
This is mainly an annotation change. If the annotation is not used correctly, all kinds of strange problems will be reported, such as the applicationContext cannot be found, the datasource instantiation fails, etc.
In order to support the above two annotations, maven files need to use pair dependencies and versions. When I added SpringRunner. Class dependency JAR, because of the auto-imported IDEA,IDE automatically imported 3.x, should actually import 4.x. I always thought that idea was imported correctly, so it took a lot of time. Later, I wrote it manually to solve the problem. Here are the correct Maven dependencies for Spring Boot Test.
【 References 】