首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring restdocs webtestclient忽略自定义jackson模块

spring restdocs webtestclient忽略自定义jackson模块
EN

Stack Overflow用户
提问于 2018-06-13 02:44:20
回答 1查看 1K关注 0票数 0

我的系统

  • 弹簧引导版2.0.1.RELEASE
  • spring版本Finchley.M9
  • java 1.8
  • org.springframework.boot:spring-boot-starter-webflux
  • org.javamoney:moneta:1.2.1
  • org.springframework.restdocs:spring-restdocs-webtestclient

自定义SimpleModule

代码语言:javascript
复制
  @Bean
  public SimpleModule moneyModule() {
    return new MoneyModule();
  }

  public MoneyModule() {
      addSerializer(Money.class, new MoneySerializer());
      addValueInstantiator(Money.class, new MoneyInstantiator());
  }

集成试验

代码语言:javascript
复制
@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();
}

试验误差

代码语言:javascript
复制
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)

当我删除以下代码时,测试代码正在正常工作

代码语言:javascript
复制
this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
        .configureClient()
        .baseUrl("http://local.com.cn")
        .filter(WebTestClientRestDocumentation
            .documentationConfiguration(this.restDocumentation)
            .operationPreprocessors()
            .withResponseDefaults(prettyPrint())
        )
        .build();

我想webtestclient的配置导致它忽略了定制的jackson模块,所以我想知道如何解决这个问题。也许根本没有问题,但我的配置是错误的。请给我一些建议。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-14 09:44:36

在使用Spring时,我建议您使用@AutoConfigureRestDocs,而不是手动配置WebTestClient以使用REST文档。这是获得使用您的自定义模块的Jackson ObjectMapper配置的ObjectMapper的最简单方法。

要做到这一点,您的测试类应该如下所示:

代码语言:javascript
复制
@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();
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50828384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档