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

slawalata picture slawalata · Jun 23, 2016 · Viewed 7.3k times · Source

I have a simple test to my RestController. I expect that $[1].parent_idreturns Long as an object and not integer primitive. It will return Long if parent_id is in a long number range and > integer number range (such as : 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

@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);
    }
}

Answer

Sam Brannen picture Sam Brannen · Jun 24, 2016

Update

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

Original Answer

One option (which I haven not personally verified) would be to try a different JsonProvider. This can be set via com.jayway.jsonpath.Configuration.setDefaults(Defaults).

If you are sure that the Long can always be safely narrowed to an int, you could 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 a Long before performing the actual matching.