I have a simple test to my RestController. I expect that $[1].parent_id
returns 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);
}
}
MockRestServiceServer
.
MockMvc
.
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.