簡介
對(duì)于普通的方法,通常采用斷言測(cè)試。
對(duì)于接口,需要使用mockMvc
對(duì)于未開發(fā)的功能,需要mockBean模擬一個(gè)業(yè)務(wù)bean
Assert
java自身攜帶的工具類,也可以用于一些對(duì)拋出異常要求不高的業(yè)務(wù)或者存在全局異常的項(xiàng)目
Assert.notNull(in,"入?yún)⒉淮嬖?);
另外有一個(gè)更加簡單的寫法,以assert開頭
assert method != null;
mockMVC
曾使用注入方式得到mockMvc,類加上@WebMvcTest注解,實(shí)際測(cè)試發(fā)現(xiàn)這樣回影響service bean的注入。
@Slf4j
@SpringBootTest
public class MvcTest {
private MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@SneakyThrows
@Test
void testGetOne(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
TestGeOne in = new TestGeOne();
in.setId(1);
JSONObject jsonObject = new JSONObject();
mockMvc.perform(MockMvcRequestBuilders.post("/test/getOne").content(JSON.toJSONBytes(in)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpectAll(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON),
MockMvcResultMatchers.jsonPath("$.data.id",Is.is(1)),
MockMvcResultMatchers.jsonPath("$.data.name").isString())
.andDo(MockMvcResultHandlers.print());
}
}
優(yōu)化
添加BeforeEach注解,用于每次初始化mock
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@MockBean
private TestServcie testServcie;
//在每個(gè)測(cè)試方法執(zhí)行之前都初始化MockMvc對(duì)象
@BeforeEach
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
mockBean(模擬bean和測(cè)試接口)
關(guān)鍵引用import static org.mockito.Mockito.*;
否則使用Mockito.when
另外就是在接口或者實(shí)現(xiàn)方法上加MockBean,這里注解可以放接口也可以具體的實(shí)現(xiàn)service。
注意mock(TestGetOneRes.class);
實(shí)際測(cè)試中存在問題文章來源:http://www.zghlxwxcb.cn/news/detail-401280.html
@Slf4j
@SpringBootTest
public class MockBeanTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@MockBean
private TestServcie testServcie;
//在每個(gè)測(cè)試方法執(zhí)行之前都初始化MockMvc對(duì)象
@BeforeEach
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
TestGetOneRes testGetOneRes = new TestGetOneRes();
testGetOneRes.setId(1);
testGetOneRes.setName("mock 測(cè)試");
//靜態(tài)導(dǎo)入import static org.mockito.Mockito.*;才能使用when方法
when(testServcie.getOne(1)).thenReturn(testGetOneRes);
//另一種寫法
TestGetOneRes testGetOneRes2 = new TestGetOneRes();
testGetOneRes2.setId(2);
doReturn(testGetOneRes2).when(testServcie).getOne(2);
}
@SneakyThrows
@Test
void testGetOne(){
TestGeOne in = new TestGeOne();
in.setId(1);
Assert.notNull(in,"入?yún)⒉淮嬖?);
mockMvc.perform(MockMvcRequestBuilders.post("/test/getOne").content(JSON.toJSONBytes(in)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpectAll(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON),
MockMvcResultMatchers.jsonPath("$.data.id", Is.is(1)),
MockMvcResultMatchers.jsonPath("$.data.name").isString())
.andDo(MockMvcResultHandlers.print());
}
}
當(dāng)出現(xiàn)多個(gè)mock都符合條件的時(shí)候,一般是最后一條mock生效。
但是如果中間出現(xiàn)拋異常,就直接返回了文章來源地址http://www.zghlxwxcb.cn/news/detail-401280.html
//入?yún)⒌扔谀硞€(gè)值
Mockito.when(testServcie.getOne(Mockito.eq(1))).thenReturn(testGetOneRes);
//任意值
Mockito.when(testServcie.getOne(Mockito.any())).thenReturn(testGetOneRes);
//拋異常
Mockito.when(testServcie.getOne(Mockito.isNotNull())).thenThrow(MyException.class);
//自定義
Mockito.when(testServcie.getOne(Mockito.anyInt())).thenAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Method method = invocationOnMock.getMethod();
Object[] arguments = invocationOnMock.getArguments();
//這里是因?yàn)橹挥幸粋€(gè)入?yún)?,所以就直接使用第一參?shù)
Integer id = (Integer)arguments[0];
TestGetOneRes testGetOneRes = new TestGetOneRes();
testGetOneRes.setId(id);
testGetOneRes.setName(id + ":" + new Date().toString());
return testGetOneRes;
}
});
到了這里,關(guān)于java的單元測(cè)試-mock測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!