MockMVC返回了空的正文

时间:2019-04-28 11:07:38

标签: java junit mockmvc

当我模拟通过有效的id时,测试运行成功,但是当我通过无效的id时,返回返回404,这是预期的,但是没有响应,当我正常执行Http Request时,它返回带有消息的响应主体找不到,状态为404。我在这里做什么错了?

@RunWith(MockitoJUnitRunner.class)
public class UserControllerTest
{
    private MockMvc mockMvc;

    @Mock
    private UserService userService;

    @InjectMocks
    private UserController userController;


    private MappingJackson2HttpMessageConverter jacksonMessageConverter;

    private ObjectMapper objectMapper;

    private User user;


    @Before
    public void setUp() 
    {

        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.setDateFormat(new ISO8601DateFormat());
        jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
        jacksonMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.valueOf("application/json")));
        jacksonMessageConverter.setObjectMapper(objectMapper);


        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders
                .standaloneSetup(userController)
                .setControllerAdvice(new ExceptionHandlerAdvice())
                .setMessageConverters(jacksonMessageConverter)
                .build();
        user = createTestUserEntity();
    }

    private User createTestUserEntity()
    {
                // create and return user block
    }

    @Test
    public void testGetReturnSuccessWhenParamIsValid() throws Exception
    {
                 // this code here returns normally json body with user object in it
    }

    @Test
    public void testGetReturnErrorWhenUserIdLessThan0() throws Exception
    {
               given(userService.getUser(anyObject())).willReturn(user);
           this.mockMvc.perform(get("/user/-1")
            .contentType(MediaType.APPLICATION_JSON_UTF8))
            .andDo(print())
            .andExpect(status().isNotFound())
            .andExpect(jsonPath("$.status").value(404))
            .andExpect(jsonPath("$.message").value("not found"))
            .andReturn();
    }



在方法testGetReturnErrorWhenUserIdLessThan0()中,应该有状态为404且没有消息的响应正文,但是当我测试时,它返回空。

以下是结果日志:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /user/-1
       Parameters = {}
          Headers = {Content-Type=[application/json;charset=UTF-8]}

Handler:
             Type = null

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: No value at JSON path "$.status": java.lang.IllegalArgumentException: json can not be null or empty

    at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:244)
    at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:99)
    at org.springframework.test.web.servlet.result.JsonPathResultMatchers$3.match(JsonPathResultMatchers.java:124)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:164)
    at com.examaple.test.api.controllers.UserControllerTest.testGetReturnErrorWhenUserIdLessThan0(UserControllerTest.java:242)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

响应应如下所示:

{
    "status": 404,
    "message": "not found"
}

控制器类:

@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @GetMapping("/{userId:\\d+}")
    public ResourceResponse<UserView> getUser(@PathVariable("userId") String _id)
            throws ResourceNotFoundException
    {
        UserId userId;
        try {
            userId = UserId.of(_id);
        } catch (IllegalArgumentException _e) {
            throw new ResourceNotFoundException("not found");
        }
        User user = userService.getUser(userId);
        return new ResourceResponse<>(new UserView(user));
    }
}

UserId类别:

@EqualsAndHashCode
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class UserId
{

    @NonNull
    private Long userId;

    public static UserId of(@NonNull String _userId)
    {
        return User.of(Long.parseLong(_userId));
    }

    public static UserId of(@NonNull  Long _userId)
    {
        if (_userId <= 0L) {
            throw new IllegalArgumentException(String.format("Param 'userId' is not valid; Got %s", _userId));
        }
        return new UserId(_userId);
    }

    @Override
    public String toString()
    {
        return userId.toString();
    }

    public Long toLong()
    {
        return userId;
    }
}

2 个答案:

答案 0 :(得分:0)

您在控制器方法中收到的ID是一个String,因此Java将在UserId类中使用String方法。如您所见,该方法的实现永远不会产生IllegalArgumentException,这就是测试失败的原因。

关于代码质量,这里有很多要说的,但是我认为最重要的是您了解为什么在这种情况下,此代码将永远无法产生预期的测试结果。

答案 1 :(得分:0)

您的请求spark.sql("select case when email = '' then 'NA' else email end from temp") 与您认为匹配的处理程序映射/user/-1不匹配。

@GetMapping("/{userId:\\d+}")是:

  

数字:[0-9]

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html