曹耘豪的博客

Mockito和JUnit5

  1. 代码

代码

1
2
3
4
5
6
7
8
9
10
11
12
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mock;

public class AbstractComponentTest {

@BeforeEach
public void _base() {
services = new Services();

services.order = orderService;
}
}

子类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class) // 初始化@Mock注解的类
@MockitoSettings(strictness = Strictness.LENIENT) // mockito的mode
public class BizComponentTest extends AbstractComponentTest {

BizService bizService;

@BeforeEach
public void init() {
bizService = new BizService();

when(bizService.doSomething(any(), any())).thenReturn(null);
}

@Test
public void test_method1() {
assertNotNull(true);
}

}
   /