@RestController
public class UserRestController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user/activate",method = RequestMethod.POST)
public ResponseEntityaram(required = true) final String email,@RequestParam(required = true) final String key) {
UserDTO userDTO = userService.activateAccount(email,key);
return new ResponseEntity
当我使用Postman调用它并且我不发送’key’参数时,我收到此JSON消息:
{
"timestamp": 1446211575193,"status": 400,"error": "Bad Request","exception": "org.springframework.web.bind.MissingServletRequestParameterException","message": "required String parameter 'key' is not present","path": "/user/activate"
}
另一方面,我正在使用JUnit和mockmvc Spring实用程序测试此方法.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationConfig.class)
@WebAppConfiguration
public class UserRestControllerTest {
private static mockmvc mockmvc;
@Mock
private UserService userService;
@InjectMocks
private UserRestController userRestController;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockmvc = mockmvcBuilders
.standalonesetup(userRestController)
.setMessageConverters(
new MappingJackson2HttpMessageConverter(),new Jaxb2RootElementHttpMessageConverter()).build();
}
@Test
public void testActivaterequiredParams() throws Exception {
mockmvc.perform(
mockmvcRequestBuilders.post("/user/activate")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON))
.andDo(mockmvcResultHandlers.print())
.andExpect(mockmvcResultMatchers.status().isBadRequest())
.andExpect(
mockmvcResultMatchers.content().contentType(
UtilsUnitTest.APPLICATION_JSON_UTF8))
.andExpect(
jsonPath(
"message",is("required String parameter 'email' is not present")));
}
}
但是当我执行此测试时,我注意到响应不是JSON消息.事实上,我得到一个例外:
java.lang.AssertionError: Content type not set
特别是,完成的结果是
MockHttpServletRequest:
HTTP Method = POST
Request URI = /user/activate
Parameters = {}
Headers = {Content-Type=[application/x-www-form-urlencoded]}
Handler:
Type = com.company.controller.UserRestController
Method = public org.springframework.http.ResponseEntityjava.lang.String,java.lang.String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.web.bind.MissingServletRequestParameterException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
MockHttpServletResponse:
Status = 400
Error message = required String parameter 'email' is not present
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
我推断当抛出异常时,这不会转换为JSON格式(当我发送电子邮件和关键参数时,我得到了正确的JSON结果)
最佳答案
问题是在断言失败后测试停止:
.andExpect(mockmvcResultMatchers.content().contentType(UtilsUnitTest.APPLICATION_JSON_UTF8))
除此之外,jsonPath(“message”…不会点击任何内容.要验证返回的错误消息,请使用mockmvcResultMatchers.status().reason(< ResultMatcher>).
以下测试应该完成这项工作:
@Test
public void testActivaterequiredParams() throws Exception {
final ResultActions result = mockmvc
.perform(mockmvcRequestBuilders.post("/user/activate")
.contentType(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON))
.andDo(mockmvcResultHandlers.print());
result.andExpect(mockmvcResultMatchers.status().isBadRequest());
result.andExpect(mockmvcResultMatchers.status().reason(is("required String parameter 'email' is not present")));
}
但是考虑一下这个(测试错误信息)是不是一个好主意,也许看看this discussion.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。