当结果Object/json包含Long字段类型时,单元测试Spring MVC REST控制器

Ken*_*Ken 4 java rest json spring-mvc spring-mvc-test

尝试使用MockMvcResultMatchers从Spring REST服务测试JSON输出时遇到问题,其中返回的对象应包含Long值.

只有当JSON对象中的值高于Integer.MAX_VALUE时,测试才会通过.这对我来说有点奇怪,因为我觉得我应该能够测试所有适用的值.

据我所知,由于JSON不包含类型信息,因此它在反序列化时对类型进行了最佳猜测,但我希望在MockMvcResultMatchers中执行比较时,有一种强制提取类型的方法.

完整代码如下,但测试是:

@Test
public void testGetObjectWithLong() throws Exception {
    Long id = 45l;

    ObjectWithLong objWithLong = new ObjectWithLong(id);

    Mockito.when(service.getObjectWithLong(String.valueOf(id))).thenReturn(objWithLong);

    mockMvc.perform(MockMvcRequestBuilders.get("/Test/" + id))
    .andExpect(MockMvcResultMatchers.status().isOk())
    .andExpect(MockMvcResultMatchers.jsonPath("$longvalue")
        .value(Matchers.isA(Long.class)))
    .andExpect(MockMvcResultMatchers.jsonPath("$longvalue")
        .value(Matchers.equalTo(id)));
}
Run Code Online (Sandbox Code Playgroud)

结果是:

java.lang.AssertionError: JSON path$longvalue
Expected: is an instance of java.lang.Long
   but: <45> is a java.lang.Integer
at org.springframework.test.util.MatcherAssertionErrors.assertThat(MatcherAssertionErrors.java:80)
...
Run Code Online (Sandbox Code Playgroud)

任何关于解决这个问题的正确方法的想法或建议都将受到赞赏.显然,我可以将Integer.MAX_VALUE添加到测试中的id字段,但这看起来很脆弱.

提前致谢.

除第三方库外,以下内容应该是自包含的

import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RunWith(MockitoJUnitRunner.class)
public class TestControllerTest {

    private MockMvc mockMvc;

    @Mock
    private RandomService service;

    @InjectMocks
    private TestController controller = new TestController();

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(controller)
            .setMessageConverters(new MappingJackson2HttpMessageConverter())
            .build();
    }

    @Test
    public void testGetObjectWithLong() throws Exception {
        Long id = 45l;

        ObjectWithLong objWithLong = new ObjectWithLong(id);

        Mockito.when(service.getObjectWithLong(String.valueOf(id))).thenReturn(objWithLong);

        mockMvc.perform(MockMvcRequestBuilders.get("/Test/" + id))
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andExpect(MockMvcResultMatchers.jsonPath("$longvalue").value(Matchers.isA(Long.class)))
        .andExpect(MockMvcResultMatchers.jsonPath("$longvalue").value(Matchers.equalTo(id)));
    }

    @RestController
    @RequestMapping(value = "/Test")
    private class TestController {

        @Autowired
        private RandomService service;

        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public ObjectWithLong getObjectWithLong(@PathVariable final String id) {
            return service.getObjectWithLong(id);
        }
    }

    @Service
    private class RandomService {
        public ObjectWithLong getObjectWithLong(String id) {
            return new ObjectWithLong(Long.valueOf(id));
        }
    }

    private class ObjectWithLong {

        private Long longvalue;

        public ObjectWithLong(final Long theValue) {
            this.longvalue = theValue;
        }

        public Long getLongvalue() {
            return longvalue;
        }

        public void setLongvalue(Long longvalue) {
            this.longvalue = longvalue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mas*_*ave 6

你可以使用anyOf匹配器 和Class超级类的Class匹配,并将其设置为

.andExpect(MockMvcResultMatchers.jsonPath("$longvalue")
        .value(Matchers.isA(Number.class)))
.andExpect(MockMvcResultMatchers.jsonPath("$longvalue")
        .value(Matchers.anyOf(
            Matchers.equalTo((Number) id),
            Matchers.equalTo((Number) id.intValue()))));
Run Code Online (Sandbox Code Playgroud)