Java 模拟单元测试
我想测试我的API,我使用一个模拟。我有一种方法来测试API的良好性能:Java 模拟单元测试,java,spring,testing,mocking,mockito,Java,Spring,Testing,Mocking,Mockito,我想测试我的API,我使用一个模拟。我有一种方法来测试API的良好性能: @Test @Transactional public void requestShouldReturnCode200() throws Exception { restMeteoMockMvc.perform(get("/api/myAPI/{param1},{param2}", GOOD_PARAM1, GOOD_PARAM2) .acc
@Test
@Transactional
public void requestShouldReturnCode200() throws Exception {
restMeteoMockMvc.perform(get("/api/myAPI/{param1},{param2}", GOOD_PARAM1, GOOD_PARAM2)
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
我想要测试的方法的签名是:
@RequestMapping(value = "/api/myAPI/{param1},{param2}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> get(@PathVariable Float param1, @PathVariable Float param2)
@RequestMapping(value=“/api/myAPI/{param1},{param2}”,
method=RequestMethod.GET,
products=MediaType.APPLICATION\u JSON\u值)
公共响应获取(@PathVariable Float param1,@PathVariable Float param2)
我想我在发送参数的方法上犯了一个错误。。。不是吗?
因为在测试结果的重定向文件中,我可以看到requestShouldReturnCode200收到了400代码,预期为200
requestShouldReturnCode200(com.bookstore.web.rest.MyClassResourceTest) Time elapsed: 0.032 sec <<< FAILURE!
java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:653)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:152)
at com.bookstore.web.rest. com.bookstore.web.rest.MyClassResourceTest.requestShouldReturnCode200(com.bookstore.web.rest.MyClassResourceTest.java:60)
requestShouldReturnCode200(com.bookstore.web.rest.MyClassResourceTest)运行时间:0.032秒您是否收到任何错误?您为什么认为自己犯了错误?你期望的结果是什么?你实际得到了什么?另外,您是否使用Mockito或PowerMock之类的模拟框架?我编辑以发布错误,是的,我使用Mockito方法和控制器类上的RequestMapping注释是什么?我们无法从方法签名猜出URL。了解作为方法参数发送的实际值(GOOD_PARAM1,GOOD_PARAM2)也很重要。这些是花车吗?