SpringBoot教程(13) JUnit5详解 常用注解 BeforeEach BeforeAll ParameterizedTest RepeatedTest

一、前言

前几个月我跳槽,入职了一家软件外包公司。虽然薪资很低,但好在不用加班。项目是个外国的,给我最大的感觉就是老外很重视UT,覆盖率要80%以上。所以开发工作中写UT也是很重要的工作。

由于我之前待过的几家公司是民企,对UT并不重视,而且我个人也没有特地学UT。虽然从大学就接触JUnit了,但是只停留在会用@Test这个水平,现在是时候学习下JUnit了。因为目前新的SpringBoot用的JUnit5,所以直接看JUnit5,JUnit4和JUnit5有不少差异。

当然UT工具不仅仅是JUnit,SpringBoot还集成了mockito、hamcrest等工具。

1. 引入test包

现在的项目基本使用SpringBoot,所以直接引入spring-boot-starter-test,

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

二、注解

注解说明
@SpringBootTestSpringBoot的注解,修饰在类上,程序会找到@SpringBootApplication修饰的主启动类,创建ApplicationContext容器。
@ExtendWith(1)当你测试类用到Spring时
Spring boot 2.1.x之前,@SpringBootTest 需要配合@ExtendWith(SpringExtension.class)才能正常工作的。
Spring Boot 2.1.x之后,@SpringBootTest里包含了@ExtendWith(SpringExtension.class),就无需再加@ExtendWith注解。
(2)当你测试类不用Spring时
如果您只想涉及Mockito而不必涉及Spring,那么当您只想使用@Mock、@InjectMocks注解时,您就得使用@ExtendWith(MockitoExtension.class),因为它不会加载到很多不需要的Spring东西中。
@Test修饰在方法上,表示这个方法是一个测试方法
@ParameterizedTest修饰在方法上,进行参数化测试。对同一个测试方法,可以有多种入参对其测试,要定义一个参数源。
@RepeatedTest修饰在方法上,表示会自动重复测试这个方法,比如@RepeatedTest(10),会自动执行10次。
@DisplayName修饰在类或者方法上,指定测试方法的展示名称
@DisplayNameGeneration修饰在类上,展示名称生成器。设置一定规则生成方法的展示名称。
例如:方法if_it_is_zero()展示出的名字为if it is zero,去掉了其中的下划线。
@BeforeEach修饰在方法上,在每一个测试方法(所有@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法)之前执行一次。
例如:一个测试类有2个测试方法testA()和testB(),还有一个@BeforeEach的方法,执行这个测试类,@BeforeEach的方法会在testA()之前执行一次,在testB()之前又执行一次。@BeforeEach的方法一共执行了2次。
@AfterEach修饰在方法上,和@BeforeEach对应,在每一个测试方法(所有@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法)之后执行一次。
@BeforeAll修饰在方法上,使用该注解的方法在当前整个测试类中所有的测试方法之前执行,每个测试类运行时只会执行一次。
@AfterAll修饰在方法上,与@BeforeAll对应,使用该注解的方法在当前测试类中所有测试方法都执行完毕后执行的,每个测试类运行时只会执行一次。
@Nested使用了这个注解的测试类表示这是一个嵌套的测试类,什么是嵌套测试类呢通俗的说就是在测试类中嵌套一个非静态的测试类(即内部类),并且可以嵌套多层。
@Tag修饰在类名或方法上,通过 @Tag 对测试类和方法进行打标签。
例如打上product表示只在生产环境执行。
@Disabled修饰在类名或方法上,表示不用执行它。
@Timeout修饰方法上,表示超过多少时间,方法还没有执行完,就判定测试失败。

三、测试案例

1. @BeforeAll、@AfterAll、@BeforeEach、@AfterEach

@ExtendWith(MockitoExtension.class)
public class DictTypeControllerTest {

    @BeforeAll
    static void testBeforeAll() {
        System.out.println("BeforeAll");
    }

    @BeforeEach
    void testBeforeEach() {
        System.out.println("BeforeEach");
    }

    @AfterEach
    void testAfterEach() {
        System.out.println("AfterEach");
    }

    @Test
    @DisplayName("方法A")
    void testA() {
        System.out.println("testA");
    }


    @Test
    @DisplayName("方法B")
    void testB() {
        System.out.println("testB");
    }

    @Test
    @DisplayName("方法C")
    @Disabled
    void testC() {
        System.out.println("testC");
    }

    @AfterAll
    static void testAfterAll() {
        System.out.println("AfterAll");
    }
}

在这里插入图片描述

2. @ParameterizedTest

@ExtendWith(MockitoExtension.class)
public class ParameterizedAnnoTest {

    @ParameterizedTest
    @MethodSource("getParams")
    void testAdd(int a, int b) {
        System.out.println(a + b);
    }

    private static Stream<Arguments> getParams() {
        return Stream.of(
                Arguments.of(1, 2),
                Arguments.of(3, 2)
        );
    }
}

3. @RepeatedTest

@ExtendWith(MockitoExtension.class)
public class RepeatedAnnoTest {
    
    @RepeatedTest(3)
    @DisplayName("方法A")
    void testA() {
        System.out.println("testA");
    }
}

结果显示执行了3次

testA
testA
testA

如果@Test和@RepeatedTest(3)同时都修饰了方法,那么会叠加,总执行4次。

@ExtendWith(MockitoExtension.class)
public class RepeatedAnnoTest {
	
	//这里多了个@Test
    @Test
    @RepeatedTest(3)
    @DisplayName("方法A")
    void testA() {
        System.out.println("testA");
    }
}

结果显示执行了4次

testA
testA
testA
testA
  • 11
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是Spring Boot Unit Test的详细文档和常用注解介绍: 1. 创建测试类 在你的测试包中创建一个测试类,你可以使用JUnit或者其他测试框架。在测试类中添加注解`@RunWith(SpringRunner.class)`和`@SpringBootTest`,如下所示: ```java @RunWith(SpringRunner.class) @SpringBootTest public class YourTestClassName { // your test methods here } ``` 2. 编写测试方法 在测试类中编写测试方法,使用注解`@Test`标记这个方法为测试方法。你可以使用Spring的依赖注入和其他功能来编写你的测试方法。 ```java @Test public void yourTestMethod() { // your test logic here } ``` 3. 常用注解介绍 - @RunWith(SpringRunner.class):指定JUnit使用Spring的测试支持。 - @SpringBootTest:在Spring环境中测试代码。 - @MockBean:用于创建一个Mock对象并注入到Spring容器中。 - @Autowired:自动注入Spring容器中的Bean。 - @Test:标记一个方法为测试方法。 - @Before:在测试方法执行之前执行。 - @After:在测试方法执行之后执行。 - @BeforeClass:在测试类中所有测试方法执行之前执行,只执行一次。 - @AfterClass:在测试类中所有测试方法执行之后执行,只执行一次。 - @DataJpaTest:在测试环境中使用Spring Data JPA测试代码。 - @WebMvcTest:在测试环境中使用Spring MVC测试代码。 - @RestClientTest:在测试环境中使用Spring RestTemplate测试代码。 4. 使用Mockito进行Mock测试 在Spring Boot Unit Test中,我们可以使用Mockito框架来进行Mock测试。Mockito可以模拟一些外部依赖,例如数据库或其他服务,使得测试更加方便和快速。下面是一个使用Mockito进行Mock测试的示例: ```java @RunWith(SpringRunner.class) @SpringBootTest public class YourTestClassName { @MockBean private YourService yourService; // mock YourService @Autowired private YourController yourController; // inject YourController @Test public void yourTestMethod() throws Exception { // mock YourService's method when(yourService.yourMethod(anyString())).thenReturn("mock result"); // call YourController's method String result = yourController.yourMethod("test"); // verify the result assertEquals("mock result", result); } } ``` 在这个例子中,我们将YourService模拟为一个MockBean,并将其注入到YourController中。然后,我们使用Mockito的when和thenReturn方法模拟YourService的方法,并调用YourController的方法测试结果。 这就是Spring Boot Unit Test的详细文档和常用注解介绍。通过这些测试,你可以更加自信地开发和部署你的Spring Boot应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为啥总是用户昵称已存在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值