我的系统
2.0.1.RELEASEFinchley.M9自定义SimpleModule
@Bean
public SimpleModule moneyModule() {
return new MoneyModule();
}
public MoneyModule() {
addSerializer(Money.class, new MoneySerializer());
addValueInstantiator(Money.class, new MoneyInstantiator());
}集成试验
@RunWith(SpringRunner.class)
@ActiveProfiles("integTest")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class XxxxHandlerTest{
@Autowired
WebTestClient webTestClient;
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
@Autowired
ApplicationContext context;
@Before
public void init() throws Exception {
this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
.configureClient()
.baseUrl("http://local.com.cn")
.filter(WebTestClientRestDocumentation
.documentationConfiguration(this.restDocumentation)
.operationPreprocessors()
.withResponseDefaults(prettyPrint())
)
.build();
}
@Test
public void testStoreVoucher() {
Operator mockUser = Operator.builder().name("jack").id(ObjectId.get().toString()).build();
List<AccountingEntry> accountingEntries = Arrays.asList(AccountingEntry.builder()
.code("12121").summary("receipt").debit(Money.of(100, "CNY"))
.credit(Money.of(100, "CNY")).build());
VoucherPost voucherPost = VoucherPost.builder().operator(mockUser)
.accountingEntries(accountingEntries).build();
webTestClient.post()
.uri(mockUrl)
.body(BodyInserters.fromObject(voucherPost))
.exchange().expectStatus().isOk();
}试验误差
org.springframework.core.codec.DecodingException: JSON decoding error: Cannot construct instance of `org.javamoney.moneta.Money` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.javamoney.moneta.Money` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)当我删除以下代码时,测试代码正在正常工作
this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
.configureClient()
.baseUrl("http://local.com.cn")
.filter(WebTestClientRestDocumentation
.documentationConfiguration(this.restDocumentation)
.operationPreprocessors()
.withResponseDefaults(prettyPrint())
)
.build();我想webtestclient的配置导致它忽略了定制的jackson模块,所以我想知道如何解决这个问题。也许根本没有问题,但我的配置是错误的。请给我一些建议。谢谢。
发布于 2018-06-14 09:44:36
在使用Spring时,我建议您使用@AutoConfigureRestDocs,而不是手动配置WebTestClient以使用REST文档。这是获得使用您的自定义模块的Jackson ObjectMapper配置的ObjectMapper的最简单方法。
要做到这一点,您的测试类应该如下所示:
@RunWith(SpringRunner.class)
@ActiveProfiles("integTest")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureRestDocs(uriHost="local.com.cn")
public class XxxxHandlerTest{
@Autowired
WebTestClient webTestClient;
@Test
public void testStoreVoucher() {
Operator mockUser = Operator.builder().name("jack").id(ObjectId.get().toString()).build();
List<AccountingEntry> accountingEntries = Arrays.asList(AccountingEntry.builder()
.code("12121").summary("receipt").debit(Money.of(100, "CNY"))
.credit(Money.of(100, "CNY")).build());
VoucherPost voucherPost = VoucherPost.builder().operator(mockUser)
.accountingEntries(accountingEntries).build();
webTestClient.post()
.uri(mockUrl)
.body(BodyInserters.fromObject(voucherPost))
.exchange().expectStatus().isOk();
}
}https://stackoverflow.com/questions/50828384
复制相似问题