小编sla*_*ata的帖子

如何强制`.andExpect(jsonPath()`返回Long/long而不是int for int number for jackson parser

我对我的RestController进行了简单的测试.我希望$[1].parent_id返回Long作为对象而不是整数原语.如果parent_id在长数字范围和>整数范围内(例如:2147483650L),它将返回Long .

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@WebAppConfiguration
public class TransactionServiceControllerTest {

@Before
public void setup() throws Exception {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    // I copy this from my RestController class
    this.transactions = Arrays.asList(
            new Transaction(100d, "car", null),
            new Transaction(100d, "table", 12L)
     );
}

@Test
public void readSingleBookmark() throws Exception {
   mockMvc.perform(MockMvcRequestBuilders.get("/transaction/"))
   .andExpect(content().contentType(contentType)) // ok
   .andExpect(jsonPath("$", hasSize(2))) // ok
   //omitted
   andExpect(jsonPath("$[1].parent_id",is(this.transactions.get(1).getParentId())));
} //assertion fail

Expected: is <12L>
but: was <12>
Run Code Online (Sandbox Code Playgroud)

另一项测试的结果:

Expected: is <12L>
but: was <2147483650L> …
Run Code Online (Sandbox Code Playgroud)

json spring-test jackson spring-rest

11
推荐指数
2
解决办法
4340
查看次数

标签 统计

jackson ×1

json ×1

spring-rest ×1

spring-test ×1