junit4/5 getDefaultClassLoader()Ljava/lang/ClassLoader;

问题出现原因

最终解决通过 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);

    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值