gpt4 book ai didi

json - 如何强制`.andExpect(jsonPath()`)返回Long/long而不是int以获取 jackson 解析器的int数

转载 作者:行者123 更新时间:2023-12-04 02:09:25 24 4
gpt4 key购买 nike

我对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>


另一个测试的结果:

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


这是我的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;
}
}


还有我的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)
);
}
}


和Application.java

@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}

最佳答案

更新资料


Spring Framework 4.3.3和5.0.0添加了对对与MockRestServiceServer一起使用的请求内容进行显式转换的一流支持。


有关详细信息,请参见SPR-14498

Spring Framework 4.3.15和5.0.5将为对与MockMvc一起使用的响应内容的显式转换添加一流的支持。


有关详细信息,请参见SPR-16587





原始答案

一种选择(我尚未亲自验证)是尝试使用其他JsonProvider。可以通过com.jayway.jsonpath.Configuration.setDefaults(Defaults)设置。

如果确定始终可以安全地将Long缩小为int,则可以使用以下方法:

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


唯一的其他选择是编写一个自定义的 Matcher,将其输入的 Integer转换为 Long,然后再执行实际的匹配。

关于json - 如何强制`.andExpect(jsonPath()`)返回Long/long而不是int以获取 jackson 解析器的int数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37996419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com