湖南公司注册网上核名,seo优化关键词挖掘,高仿做的好点的网站,哪个企业做网站目录 1. 确保项目中包含相关依赖2. 配置JUnit 53. 编写测试类4、Junit5 新增特性4.1 注解4.2 断言4.3 嵌套测试4.4 总结 在Spring Boot 3中集成JUnit 5的步骤相对简单。以下是你可以按照的步骤#xff1a;
1. 确保项目中包含相关依赖
首先#xff0c;确保你的pom.xml文件中… 目录 1. 确保项目中包含相关依赖2. 配置JUnit 53. 编写测试类4、Junit5 新增特性4.1 注解4.2 断言4.3 嵌套测试4.4 总结 在Spring Boot 3中集成JUnit 5的步骤相对简单。以下是你可以按照的步骤
1. 确保项目中包含相关依赖
首先确保你的pom.xml文件中包含了Spring Boot 3和JUnit 5相关的依赖。下面是需要的基本依赖
dependencies!-- Spring Boot Starter Test包含JUnit 5支持 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!-- JUnit 5的依赖Spring Boot 3已经集成JUnit 5 dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-api/artifactIdversion5.8.2/versionscopetest/scope/dependencydependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter-engine/artifactIdversion5.8.2/versionscopetest/scope/dependency --
/dependencies注意spring-boot-starter-test 默认包含JUnit 5支持所以你不需要显式地引入JUnit 5的依赖除非你有特定版本的需求。
2. 配置JUnit 5
Spring Boot 3默认启用了JUnit 5你只需要按照JUnit 5的方式编写测试代码即可。Spring Boot的SpringBootTest注解会与JUnit 5兼容。
3. 编写测试类
创建一个简单的JUnit 5测试类使用SpringBootTest来加载Spring Boot应用程序上下文。
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;SpringBootTest
class MyApplicationTests {Testvoid test() {// 这里写测试方法}
}4、Junit5 新增特性
4.1 注解
JUnit5的注解与JUnit4的注解有所变化 https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations ● Test :表示方法是测试方法。但是与JUnit4的Test不同他的职责非常单一不能声明任何属性拓展的测试将会由Jupiter提供额外测试 ● ParameterizedTest :表示方法是参数化测试下方会有详细介绍 ● RepeatedTest :表示方法可重复执行下方会有详细介绍 ● DisplayName :为测试类或者测试方法设置展示名称 ● BeforeEach :表示在每个单元测试之前执行 ● AfterEach :表示在每个单元测试之后执行 ● BeforeAll :表示在所有单元测试之前执行 ● AfterAll :表示在所有单元测试之后执行 ● Tag :表示单元测试类别类似于JUnit4中的Categories ● Disabled :表示测试类或测试方法不执行类似于JUnit4中的Ignore ● Timeout :表示测试方法运行如果超过了指定时间将会返回错误 ● ExtendWith :为测试类或测试方法提供扩展类引用
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;class StandardTests {BeforeAllstatic void initAll() {}BeforeEachvoid init() {}DisplayName()Testvoid succeedingTest() {}Testvoid failingTest() {fail(a failing test);}TestDisabled(for demonstration purposes)void skippedTest() {// not executed}Testvoid abortedTest() {assumeTrue(abc.contains(Z));fail(test should have been aborted);}AfterEachvoid tearDown() {}AfterAllstatic void tearDownAll() {}}4.2 断言 4.3 嵌套测试
JUnit 5 可以通过 Java 中的内部类和Nested 注解实现嵌套测试从而可以更好的把相关的测试方法组织在一起。在内部类中可以使用BeforeEach 和AfterEach 注解而且嵌套的层次没有限制。
DisplayName(A stack)
class TestingAStackDemo {StackObject stack;TestDisplayName(is instantiated with new Stack())void isInstantiatedWithNew() {new Stack();}NestedDisplayName(when new)class WhenNew {BeforeEachvoid createNewStack() {stack new Stack();}TestDisplayName(is empty)void isEmpty() {assertTrue(stack.isEmpty());}TestDisplayName(throws EmptyStackException when popped)void throwsExceptionWhenPopped() {assertThrows(EmptyStackException.class, stack::pop);}TestDisplayName(throws EmptyStackException when peeked)void throwsExceptionWhenPeeked() {assertThrows(EmptyStackException.class, stack::peek);}NestedDisplayName(after pushing an element)class AfterPushing {String anElement an element;BeforeEachvoid pushAnElement() {stack.push(anElement);}TestDisplayName(it is no longer empty)void isNotEmpty() {assertFalse(stack.isEmpty());}TestDisplayName(returns the element when popped and is empty)void returnElementWhenPopped() {assertEquals(anElement, stack.pop());assertTrue(stack.isEmpty());}TestDisplayName(returns the element when peeked but remains not empty)void returnElementWhenPeeked() {assertEquals(anElement, stack.peek());assertFalse(stack.isEmpty());}}}
}4.4
参数化测试是JUnit5很重要的一个新特性它使得用不同的参数多次运行测试成为了可能也为我们的单元测试带来许多便利。
利用ValueSource等注解指定入参我们将可以使用不同的参数进行多次单元测试而不需要每新增一个参数就新增一个单元测试省去了很多冗余代码。
ValueSource: 为参数化测试指定入参来源支持八大基础类以及String类型,Class类型 NullSource: 表示为参数化测试提供一个null的入参 EnumSource: 表示为参数化测试提供一个枚举入参 CsvFileSource表示读取指定CSV文件内容作为参数化测试入参 MethodSource表示读取指定方法的返回值作为参数化测试入参(注意方法返回需要是一个流)
ParameterizedTest
ValueSource(strings {one, two, three})
DisplayName(参数化测试1)
public void parameterizedTest1(String string) {System.out.println(string);Assertions.assertTrue(StringUtils.isNotBlank(string));
}ParameterizedTest
MethodSource(method) //指定方法名
DisplayName(方法来源参数)
public void testWithExplicitLocalMethodSource(String name) {System.out.println(name);Assertions.assertNotNull(name);
}static StreamString method() {return Stream.of(apple, banana);
}总结
Spring Boot 3已经内建了对JUnit 5的支持只要使用spring-boot-starter-test依赖即可。编写JUnit 5测试时使用SpringBootTest加载应用上下文。可以利用JUnit 5的生命周期方法、参数化测试等特性进行更精细的测试。