问题出现原因
最终解决通过 Maven Helper 查看了依赖,发现了正真测试的时候使用的是 springboot 中的 jupiter
查看了 springboot parent 中的依赖,发现以下配置
<!--parent 中的配置-->
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<!--通过上面的版本映入了 一个 bom,可能是因为这个 bom 里的依赖版本造成的问题-->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit-jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--测试项目 中的配置-->
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
<!--将测试项目中的配置修改为,即可成功运行-->
<junit-jupiter.version>5.0.0-M4</junit.jupiter.version>
`
使用 Junit 4 成功用例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demoda</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoda</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
使用 Junit 5 成功用例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demoda</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoda</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
测试例子
*/
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ServiceHospApplicationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void createUser() {
User user = new User();
user.setName("张三");
user.setAge(27);
user.setEmail("yinhaoye@qq.com");
User userMongo = mongoTemplate.insert(user);
System.out.println(userMongo);
}
}