当前位置: 首页 > news >正文

北镇建设局网站江苏网站seo

北镇建设局网站,江苏网站seo,做网站页面报价,wordpress子网页目录 1. 确保项目中包含相关依赖2. 配置JUnit 53. 编写测试类4、Junit5 新增特性4.1 注解4.2 断言4.3 嵌套测试4.4 总结 在Spring Boot 3中集成JUnit 5的步骤相对简单。以下是你可以按照的步骤: 1. 确保项目中包含相关依赖 首先,确保你的pom.xml文件中…

目录

      • 1. 确保项目中包含相关依赖
      • 2. 配置JUnit 5
      • 3. 编写测试类
      • 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支持) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- JUnit 5的依赖(Spring Boot 3已经集成JUnit 5) <dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.8.2</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.8.2</version><scope>test</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");}@Test@Disabled("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 {Stack<Object> stack;@Test@DisplayName("is instantiated with new Stack()")void isInstantiatedWithNew() {new Stack<>();}@Nested@DisplayName("when new")class WhenNew {@BeforeEachvoid createNewStack() {stack = new Stack<>();}@Test@DisplayName("is empty")void isEmpty() {assertTrue(stack.isEmpty());}@Test@DisplayName("throws EmptyStackException when popped")void throwsExceptionWhenPopped() {assertThrows(EmptyStackException.class, stack::pop);}@Test@DisplayName("throws EmptyStackException when peeked")void throwsExceptionWhenPeeked() {assertThrows(EmptyStackException.class, stack::peek);}@Nested@DisplayName("after pushing an element")class AfterPushing {String anElement = "an element";@BeforeEachvoid pushAnElement() {stack.push(anElement);}@Test@DisplayName("it is no longer empty")void isNotEmpty() {assertFalse(stack.isEmpty());}@Test@DisplayName("returns the element when popped and is empty")void returnElementWhenPopped() {assertEquals(anElement, stack.pop());assertTrue(stack.isEmpty());}@Test@DisplayName("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 Stream<String> method() {return Stream.of("apple", "banana");
}

总结

  • Spring Boot 3已经内建了对JUnit 5的支持,只要使用spring-boot-starter-test依赖即可。
  • 编写JUnit 5测试时,使用@SpringBootTest加载应用上下文。
  • 可以利用JUnit 5的生命周期方法、参数化测试等特性进行更精细的测试。
http://www.ds6.com.cn/news/27196.html

相关文章:

  • 网站会员等级审核功能怎么做邯郸seo
  • 推广网站链接怎么做seo公司排行
  • 洛可可设计公司待遇优化网站界面的工具
  • 网站制作公司美股上市深圳网络推广建站
  • 做酒店管理网站的作用邮件营销
  • php动态网站开发介绍软文推广软文营销
  • c 视频网站开发武汉网站排名提升
  • 诸暨广川建设公司网站短链接生成器
  • 手机怎么做电子书下载网站新闻发稿
  • 网站 编程 语言seo排名工具提升流量
  • 下载了模板如何做网站百度渠道开户哪里找
  • 自己建设一个网站软件seo软件系统
  • 萧山网站建设seo免费浏览网站
  • 做亚马逊网站费用国外免费舆情网站有哪些软件
  • 安阳做一个网站多少钱糕点烘焙专业培训学校
  • 苍南最好的网站建设公司线上销售如何找到精准客户
  • 网站建设哪家专业百度官网链接
  • 珠海网站建设电话100个电商平台
  • 建设公司网站的意义太原seo公司
  • 江苏网站建设渠道seo研究中心倒闭
  • 郑州优化网站公司关键词免费下载
  • 海门市政府投资项目工程建设中心网站重庆森林影评
  • 网站建设更新维护工作总结网站seo诊断报告怎么写
  • 做网站app东莞市网络seo推广服务机构
  • 专业移动网站建设seo优化顾问服务
  • 安徽国贸网站建设网店推广策划书
  • 学做日本菜的网站好百度视频排名优化
  • xmapp怎样做网站怎么查询最新网站
  • 做调查的网站知乎合肥网络seo推广服务
  • 网站怎么做不违法西安疫情最新消息1小时内