How to make `.andExpect (jsonPath ()` return Long / long instead of int for int number for jackson parser

I have a simple test for my RestController. I expect $[1].parent_id return Long as an object, not an integer primitive. It will return Long if parent_id is in a long range of numbers and> integers (e.g. 2147483650L).

 @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> 

Result from another test:

 Expected: is <12L> but: was <2147483650L> //return Long instead int 

this is my JacksonConfiguration setup

 @Configuration public class JacksonConfiguration { @Bean @Primary public ObjectMapper objectMapper() { final ObjectMapper objectMapper = new ObjectMapper(); //supposed to be this is the magic trick but it seems not.. objectMapper.enable(DeserializationFeature.USE_LONG_FOR_INTS); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); return objectMapper; } } 

And my POJO

 public class Transaction { private double ammount; private String type; private Long parentId; public Transaction(Double ammount, String type, Long parentId) { //omitted } //setter and getter omitted } 

MyRestController

 @RestController @RequestMapping("transaction") public class TransactionServiceController { @RequestMapping(method = RequestMethod.GET) List<Transaction> getTransaction() { return Arrays.asList( new Transaction(100d, "car", null), new Transaction(100d, "table", 12L) ); } } 

And Application.java

 @SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } } 
+5
source share
1 answer

Update

  • Spring Framework 4.3.3 and 5.0.0 added first-class support for explicit conversions for content requests for use with MockRestServiceServer .
  • Spring Framework 4.3.15 and 5.0.5 will add support for explicit conversions for response content for use with MockMvc .

Original answer

One option (which I personally did not personally JsonProvider ) was to try another JsonProvider . This can be set using com.jayway.jsonpath.Configuration.setDefaults(Defaults) .

If you are sure that Long can always be safely narrowed to int , you can use the following:

 andExpect(jsonPath("$[1].parent_id",is(this.transactions.get(1).getParentId().intValue()))); 

And the only other option is to write a custom Matcher that converts the incoming Integer to Long before doing the actual match.

+5
source

All Articles