【发布时间】:2021-07-26 14:47:23
【问题描述】:
这是我的控制器类的映射,现在我想为它编写单元测试用例
@GetMapping(value = "/tokenSearch/{id}/{name}/{values}/{data_name}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getValuesfromToken(
throws ClientProtocolException, IOException, ParseException {
ResponseEntity<String> data = elasticService.getData();
return data;
}
这是我正在尝试的,但它要求对结果匹配器进行castargument,出现错误,有人可以帮我解决这个问题
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class ElasticCaseTests extends Mockito {
@Autowired
private MockMvc mockMvc;
@Test
public void testGetValuesfromToken() throws Exception {
String contentAsString = mockMvc.perform(get("/tokenSearch/1/PRODUCT/PRODUCT/189")).andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("1"))).andExpect(jsonPath("$.name", is("product")))
.andExpect(jsonPath("$.values", is("product")))
.andExpect(jsonPath("$.searching_word", is("189"))).andExpect(status().isOk()).andReturn().getResponse()
.getContentAsString();
}
java.lang.AssertionError: No value at JSON path "$.id"' , can someone help me with this
【问题讨论】:
-
你在谷歌上没有找到任何东西吗?
-
如果你能证明你所付出的努力以及为什么它不起作用,你将会取得更大的成功。那么人们会更愿意提供帮助
-
当然,请检查@batman567,这样好吗
标签: java spring-boot junit mockito junit4