专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

springboot2.3+junit5 junit测试编写

依赖相关

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- Junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>

junit4

package com.imddysc.taskweb.repository;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.imddysc.taskweb.entity.Task;

@RunWith(SpringRunner.class)
@SpringBootTest
class TaskRepositoryTest2 {

    @Autowired
    private TaskRepository taskRepository;

    @Test
    void test() {
        Optional<Task> task = taskRepository.findById((long) 1);
        System.out.println("Task:: " + task.get());
    }

}

junit5(junit jupiter)

package com.imddysc.taskweb.repository;

import static org.junit.jupiter.api.Assertions.fail;

import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.imddysc.taskweb.entity.Task;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@DisplayName("TaskRepositoryTest")
class TaskRepositoryTest {

    @Autowired
    private TaskRepository taskRepository;

    @Test
    @DisplayName("新junit测试1")
    void test() {
        Optional<Task> task = taskRepository.findById((long) 1);
        System.out.println("Task:: " + task.get());
    }

}

JUnit 5跟以前的JUnit版本不一样,它由几大不同的模块组成,这些模块分别来自三个不同的子项目。

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage ##

主要注解:

一、 @BeforeAll 类似于JUnit 4的@BeforeClass表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行,必须为static

二、 @BeforeEach 类似于JUnit 4的@Before,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之前执行
三、 @Test 表示该方法是一个测试方法
四、 @DisplayName 为测试类或测试方法声明一个自定义的显示名称
五、 @AfterEach 类似于JUnit 4的@After,表示使用了该注解的方法应该在当前类中每一个使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行
六、 @AfterAll 类似于JUnit 4的@AfterClass,表示使用了该注解的方法应该在当前类中所有使用了@Test、@RepeatedTest、@ParameterizedTest或者@TestFactory注解的方法之后执行,必须为static
七、 @Disable 用于禁用一个测试类或测试方法,类似于JUnit 4的@Ignore
八、 @ExtendWith 用于注册自定义扩展 ###

文章永久链接:https://tech.souyunku.com/24754

未经允许不得转载:搜云库技术团队 » springboot2.3+junit5 junit测试编写

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们