Junittest
JUnit 5中的测试执行顺序
一般实践认为,自动化测试应能够独立运行且无特定顺序,并且测试结果不应依赖于先前测试的结果。 但是在某些情况下,可以证明特定的测试执行顺序是正确的,尤其是在集成或端到端测试中。
默认情况下,在JUnit 5中,测试方法的执行在构建之间是可重复的,因此具有确定性,但是该算法是故意不明显的(作为库状态的作者)。 幸运的是,可以使用内置方法定购器或通过创建自定义定购器来调整执行顺序以满足我们的需求。
org.junit.jupiter.api.TestMethodOrder
为了更改测试执行顺序,我们需要使用org.junit.jupiter.api.TestMethodOrder注释测试类,并将方法排序器的类型作为参数传递。 从JUnit 5.4开始,有三个内置的方法排序器: OrderAnnotation , Alphanumeric和Random 。 通过实现org.junit.jupiter.api.MethodOrderer接口,我们还可以轻松创建自己的自定义方法org.junit.jupiter.api.MethodOrderer器。
MethodOrderer.OrderAnnotation使用
1 | package pl.codeleak.samples.junit5.basics; |
MethodOrderer.Alphanumeric
1 | @TestMethodOrder (MethodOrderer.Alphanumeric. class ) |
MethodOrderer.Random
1 | @TestMethodOrder (MethodOrderer.Random. class ) class AlphanumericTestExecutionOrder { |
Mokito RestTemplate的exchange 例子
- RestTemplate 的exchange 方法若mokito 不对,因为RestTemplate 中exchange 上面方法重载有4个
1
2public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {

就会出现以下错误
1 | getting error like org.mockito.exceptions.misusing.InvalidUseOfMatchersException: |
- Mokito 例子
1
2
3
4
5Mockito.doReturn(ResponseEntity.ok(new Object )).when(restTemplate)
.exchange(Mockito.anyString(),Mockito.eq(HttpMethod.GET),
(org.springframework.http.HttpEntity<?>) Mockito.any(),
(Class<Object>) Mockito.any());
junit5 + spring boot
- 代码SpringJunitTest5 分支main,jdk11
- unit test code
junit5与junit 4不同
spring boot整合junit5 test
junit5要使用@SpringJUnitConfig和@SpringBootTest 注解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
public class HelloControllerTest {
protected MockMvc mockMvc;
protected WebApplicationContext webApplicationContext;
public void setup() throws Exception {
Assert.notNull(webApplicationContext, "'webApplicationContext' must not be null");
MockitoAnnotations.openMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
public void hello() throws Exception {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("name","world");
ResultActions resultActions = mockMvc.perform(requestBuilder);
resultActions.andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().is(200));
resultActions.andExpect(content().string("hello world"));
}
}
关于TestInstance.Lifecycle.PER_CLASS
为了允许隔离执行单个的测试方法,并避免由于可变测试实例状态而产生的意外副作用,JUnit在执行每个测试方法之前创建每个测试类的新实例 只需使用@TestInstance(Lifecycle.PER_CLASS)对您的测试类进行注解即可。当使用这种模式时,每个测试类将创建一个新的测试实例。因此,如果您的测试方法依赖于存储在实例变量中的状态,则可能需要在@BeforeEach或@AfterEach方法中重置该状态。
如果测试类或测试接口没有用@TestInstance注解,JUnit Jupiter将使用默认的生命周期模式。标准默认模式是PER_METHOD
验证插入数据库的对象数据是否正确
- 测试的目的:
程序是否调用了一次HelloDaoImpl的hello 方法
并验证传入参数值是否是world? - dao 类
1
2
3
4
5
6public class HelloDaoImpl implements IHelloDao {
public String hello(String name) {
return name;
}
} - service 类
1
2
3
4
5
6
7
8
9
10
public class HelloServiceImpl implements IHelloService {
IHelloDao helloDao;
public String hello(String name) {
helloDao.hello(name);
return name;
}
} - test verify data 类
实现VerificationMode类的verify 方法
1 | public class HelloServiceVerification implements VerificationMode { |
- unit test类
1
2
3
4
5
6
7
8
9
10
11@Test
public void hello() throws Exception {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("name","world");
ResultActions resultActions = mockMvc.perform(requestBuilder);
resultActions.andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().is(200));
resultActions.andExpect(content().string("hello world"));
Mockito.verify(helloDao, new HelloServiceVerification(1, "world")).hello(Mockito.any(String.class));
}
junit4 +spring boot
- 代码SpringbootJunit4 分支main,jdk8
- unit test code
spring boot整合junit4 test
要使用@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest注解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@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringbootJunit4Application.class)
public class HelloWorldControllerTest {
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext webApplicationContext;
@Before
public void setup() throws Exception {
Assert.notNull(webApplicationContext, "'webApplicationContext' must not be null");
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void hello() throws Exception {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("name", "world");
ResultActions resultActions = mockMvc.perform(requestBuilder);
resultActions.andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().is(200));
resultActions.andExpect(content().string("hello world"));
}
}
Spring+junit 4
- controller code ,jdk8
1
2
3
4
5
6
7
8
public class HelloController {
("/hello")
public String hello(String name){
return "hello "+name;
}
} - test code
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@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations ={"classpath*:/application-context.xml"})
public class HelloControllerTest {
@Autowired
protected WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;
@Before
public void setup() throws Exception {
Assert.notNull(webApplicationContext, "'webApplicationContext' must not be null");
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}
@Test
public void hello() throws Exception {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("name", "world");
ResultActions resultActions = mockMvc.perform(requestBuilder);
resultActions.andDo(MockMvcResultHandlers.print());
resultActions.andExpect(status().is(200));
resultActions.andExpect(content().string("hello world"));
}
}
本文标题:Junittest
文章作者:peter.tan
发布时间:2022-09-09
最后更新:2022-10-08
原始链接:https://petertanblog.github.io/2022/09/09/Junittest/
版权声明:Copyright © 2022 Peter.tan
分享
