springboot单元测试

参考文档:
JUnit 5 Jupiter API

junit4和junit5的注解比较

JUnit 5JUnit 4说明
@Test@Test被注解的方法是一个测试方法。与 JUnit 4 相同。
@BeforeAll@BeforeClass被注解的(静态)方法将在当前类中的所有 @Test 方法前执行一次。
@BeforeEach@Before被注解的方法将在当前类中的每个 @Test 方法前执行。
@AfterEach@After被注解的方法将在当前类中的每个 @Test 方法后执行。
@AfterAll@AfterClass被注解的(静态)方法将在当前类中的所有 @Test 方法后执行一次。
@Disabled@Ignore被注解的方法不会执行(将被跳过),但会报告为已执行。

spring5集成junit4

@RunWith(SpringJUnit4ClassRunner.class) //单元测试框架  使用注解
@ContextConfiguration("classpath:bean.xml")//加载配置文件 

或者

@RunWith(SpringRunner.class) //SpringRunner继承自SpringJUnit4ClassRunner
@ContextConfiguration("classpath:bean.xml")//加载配置文件 

spring5集成junit5

@ExtendWith(SpringExtension.class)//指示JUnit Jupiter(Junit5)使用Spring支持扩展测试。
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件 

或者

@SpringJUnitConfig(locations = "classpath:applicationContext.xml")

springboot2集成junit4

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

@Test 用org.junit.Test 配合@RunWith使用
在这里插入图片描述

springboot2集成junit5

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>

或者

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

包含了junit-jupiter
在这里插入图片描述
@Test 用org.junit.jupiter.api.Test
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值