Markdown语法
斜体或粗体
分级标题
代码块
```
语言 // 你的代码内容
https://blog.youkuaiyun.com/witnessai1/article/details/52551362
Junit5
Junit5 注解
注解 | 说明 |
---|---|
@Test | 表示方法是测试方法。与JUnit 4的@Test注释不同,这个注释不声明任何属性,因为JUnit Jupiter中的测试扩展基于它们自己的专用注释进行操作。 |
@ParameterizedTest | 表示方法是参数化测试。 |
@RepeatedTest | 表示方法是重复测试的测试模板。 |
@TestFactory | 表示方法是动态测试的测试工厂。 |
@TestInstance | 用于为带注释的测试类配置测试实例生命周期。 |
@TestTemplate | 表示方法是为测试用例设计的模板,根据注册提供程序返回的调用上下文的数量进行多次调用。 |
@DisplayName | 声明测试类或测试方法的自定义显示名称。 |
@BeforeEach | 表示在当前类中每个@Test、@RepeatedTest、@ParameterizedTest或@TestFactory方法之前执行注释的方法;类似于JUnit 4的@Before。 |
@AfterEach | 表示在当前类中的每个@Test、@RepeatedTest、@ParameterizedTest或@TestFactory方法之后,都应该执行带注释的方法;类似于JUnit 4的@After。 |
@BeforeAll | 表示应在当前类中的所有@Test、@RepeatedTest、@ParameterizedTest和@TestFactory方法之前执行带注释的方法;类似于JUnit 4的@BeforeClass。 |
@AfterAll | 表示在当前类中,所有@Test、@RepeatedTest、@ParameterizedTest和@TestFactory方法都应该执行注释的方法;类似于JUnit 4的@AfterClass。 |
@Nested | 表示带注释的类是一个嵌套的、非静态的测试类。@BeforeAll和@AfterAll方法不能直接在 @Nested 测试类中使用,除非使用“每个类”测试实例生命周期。 |
@Tag | 用于在类或方法级别声明过滤测试的标记;类似于TestNG中的测试组或JUnit 4中的类别。 |
@Disabled | 用于禁用测试类或测试方法;类似于JUnit 4的@Ignore。 |
@ExtendWith | 用于注册自定义扩展。 |
代码学习链接: https://www.wdbyte.com/java/junit5/
Junit5 断言(Assertions类)
mockito
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.3.1</version>
</dependency>
Return():指定返回值
thenthrow():运行时抛出指定的异常
thenCallRealMethod():调用真实的方法
@BeforeEach : @每个Test方法前调用被BeforeEach@ 注解的方法
@AfterEach : 反之
@BeforeAll :相比于@BeforeEach 只 触发一次
需要是 static 方法!
@BeforeAll
static void init() {
System.out.println("init one");
}
@AfterAll : 反之
## mockito+junit 5
```java
@Test
void add() {
Random random = Mockito.mock(Random.class);
System.out.println(random.nextInt());
Mockito.verify(random).nextInt(); //verify用于验证 mock 对象的方法是否按照期望被调用
Mockito.verify(random,Mockito.times(1) ).nextInt();
}
Mockito.mock方法
直接使用Mockito.mock方法
@Test
void add() {
Random random = Mockito.mock(Random.class);
Mockito.when(random.nextInt()).thenReturn(100);//定义调用random.nextInt()方法的值始终返回100 打桩
Assertions.assertEquals(100,random.nextInt());//断言
}
@Mock注解
@Mock注解需要与***MockitoAnnotations.openMocks(this)***方法配合使用
class demoTest {
@Mock
private Random random;
@Test
void add() {
MockitoAnnotations.openMocks(this);
Mockito.when(random.nextInt()).thenReturn(100);//定义调用random.nextInt()方法的值始终返回100 打桩
Assertions.assertEquals(100,random.nextInt());//断言
}
}
@BeforeEach和@AfterEach
class demoTest {
@Mock
private Random random;
@BeforeEach
void setup(){
System.out.println("----测试前准备----");
}
@Test
void add() {
MockitoAnnotations.openMocks(this);
Mockito.when(random.nextInt()).thenReturn(100);//定义调用random.nextInt()方法的值始终返回100
Assertions.assertEquals(100,random.nextInt());//断言
}
@AfterEach
void after(){
System.out.println("----测试结束----");
}
}
@Spy
@Spy: 调用真实方法
class demoTest {
@Spy
private Demo demo;
@BeforeEach
void setup(){
MockitoAnnotations.openMocks(this);
}
@Test
void add() {
Mockito.when(demo.add(1,2)).thenReturn(100);//定义调用random.nextInt()方法的值始终返回100
Assertions.assertEquals(3,demo.add(1,2));
}
}
@InjectMocks
将userDao等注入到写的类里面
MockitoAnnotations.openMocks 方法
开启Mock方法和Spy方法
Mockito.MockStatic 方法
需引入依赖: 不可与 mockito-core 依赖同时使用!!!
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</dependency>
public class StaticUtils {
private StaticUtils() {
}
;
public static List<Integer> range(int start, int end) {
return IntStream.range(start, end).boxed().collect(Collectors.toList());
}
public static String name() {
return "hhhh";
}
}
MockStatic方法不允许同时运行(需要关闭)!
class StaticUtilsTest {
@Test
void range() {
try (MockedStatic<StaticUtils> demo = Mockito.mockStatic(StaticUtils.class)) {
demo.when(() -> StaticUtils.range(2, 6)).thenReturn(Arrays.asList(10, 11, 12));
Assertions.assertTrue(StaticUtils.range(2, 5).contains(10));
}
}
@Test
void name() {
try (MockedStatic<StaticUtils> demo1 = Mockito.mockStatic(StaticUtils.class)) {
demo1.when(StaticUtils::name).thenReturn("hahahah");
Assertions.assertEquals("hahahah", StaticUtils.name());
}
}
}
// 在junit4的时候使用@Runwith注解,在junit5的时候使用的是@ExtendWith(MockitoExtension.class)注解
@ExtendWith(MockitoExtension.class)