Junit4 升级为Junit5 后的变化

针对于springboot版本的测试Junit4 升级为Junit5 后的变化

出现的import org.junit.jupiter.api.Test;类包找不到问题

springboot2.2版本之前的配置

package com.example.spring;
 
 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTests {
 |    @Test
    public void contextLoads() {
    }
 
}

Spring Boot 2.2 之后的测试类

package com.example.demo;
 
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest
class SpringBootApplicationTests {
 
    @Test
    void contextLoads() {
    }
}


Spring Boot 2.2 之前的 pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>   
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
         <artifactId>junit</artifactId>
            <!-- 默认的版本为3.8.1,修改为4.x,因为3.x使用的为编程的方式,4.x为注解的形式。 -->
          <version>4.12</version>
 </dependency>
</dependencies>

Spring Boot 2.2 之后的 pom.xml

```java
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

springboot2.2之前所用的则是Junit4
springboot2.2之后的则是Junit5
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JUnit 4 是JUnit测试框架的最新版本,它与JUnit 3有很多不同之处。以下是将JUnit 3升级JUnit 4的步骤: 1. 导入JUnit 4库 - 在项目的构建路径中,将JUnit 4库导入到项目中。 2. 更改测试注释 - 在JUnit 3中,测试方法使用“@Test”注释来标记。在JUnit 4中,测试方法应使用“@Test”注释来标记。 3. 更改断言语句 - 在JUnit 3中,断言语句使用“assertTrue”或“assertFalse”等方法来检查条件。在JUnit 4中,可以使用更多的断言方法,例如“assertEquals”和“assertNotNull”。 4. 更改测试套件 - 在JUnit 3中,测试套件作为一个类来实现,并且必须继承TestCase类。在JUnit 4中,测试套件可以使用“@RunWith”注释来标记,并且可以使用“@SuiteClasses”注释来指定包含的测试类。 5. 使用注释取代传统的测试方法 - 在JUnit 4中,可以使用注释来指定测试方法的预备条件,例如@Before和@After。这些注释可以用于所有测试方法,而不是只能用于继承自TestCase的测试方法。 6. 使用参数化测试 - 在JUnit 4中,可以使用“@Parameterized”注释来标记测试方法,从而使测试方法可以接受参数。这样可以更轻松地测试不同的输入和输出。 7. 使用JUnit 4规则 - JUnit 4包含许多规则(例如TemporaryFolder)来帮助测试。可以使用“@Rule”注释来标记规则。 通过这些步骤,就可以将JUnit 3升级JUnit 4,并且可以使用JUnit 4的新功能来改进测试。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值