MockMVC返回空身

问题描述 投票:1回答:2

当我模拟传递有效的id时,测试运行成功,但是当我传递无效的id时,返回返回404,这是预期的,但是没有响应,并且当我正常Http请求时,它返回响应体并且找不到消息状态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;
    }
}
java junit mockmvc
2个回答
0
投票

您的请求/user/-1与您认为它的@GetMapping("/{userId:\\d+}")处理程序映射不匹配。

\d是:

数字:[0-9]

(Qazxswpoi)


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.