查看“Junit 5”的源代码
←
Junit 5
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
要是跟 Spring Boot 2 及以上用,那么已经在 spring-boot-starter-test 里间接依赖了, 测试基类使用 @SpringBootTest(classes = Application.class) 之后,其他测试类直接继承就可以了。 然后要注意的是,区别用 Junit 4 用的 @Test 注解来自 org.junit.Test,Junit 5 的 @Test 来自 org.junit.jupiter.api.Test, 然后如果要断言静态方法的调用:<syntaxhighlight lang="java"> public class MathUtils { public static int add(int a, int b) { return a + b; } } </syntaxhighlight><syntaxhighlight lang="java"> import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; import org.mockito.Mockito; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) public class StaticMockTest { @Test public void testMockStatic() { try (MockedStatic<MathUtils> mockedMathUtils = mockStatic(MathUtils.class)) { // 模拟MathUtils.add方法的返回值 when(MathUtils.add(1, 2)).thenReturn(5); // 调用被测试的代码,假设此处有代码调用了MathUtils.add(1, 2) int result = MathUtils.add(1, 2); assertEquals(5, result); // 验证MathUtils.add方法是否被调用了一次,参数为1和2 mockedMathUtils.verify(() -> MathUtils.add(1, 2)); } } } </syntaxhighlight>需要再引入:<syntaxhighlight lang="xml"> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <scope>test</scope> </dependency> </syntaxhighlight>
返回至
Junit 5
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息