java - Spring REST 文档测试中未使用自定义 Jackson 模块

标签 java spring spring-mvc spring-boot spring-restdocs

我已经使用自定义 Jackson 模块(使用 Spring Boot 1.3)在 Spring REST 文档上编写了一个小测试。在我的应用程序主类中,我只有 @SpringBootApplication。然后我有另一个类 JacksonCustomizations 看起来像这样:

@Configuration
public class JacksonCustomizations {

@Bean
public Module myCustomModule() {
    return new MyCustomModule();
}

static class MyCustomModule extends SimpleModule {
    public MyCustomModule() {

        addSerializer(ImmutableEntityId.class, new JsonSerializer<ImmutableEntityId>() {
            @Override
            public void serialize(ImmutableEntityId immutableEntityId, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
                jsonGenerator.writeNumber( (Long)immutableEntityId.getId() );
            }
        });
    }
}
}

这个定制是完美的。当我运行 Spring Boot 应用程序时,我看到了应有的 JSON。

但是,在我的文档测试中,没有应用自定义。这是我的测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@WebAppConfiguration
public class NoteControllerDocumentation {

@Rule
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");

@Autowired
private WebApplicationContext context;

private MockMvc mockMvc;

@Before
public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.webAppContextSetup(context)
                             .apply(documentationConfiguration(restDocumentation))
                             .build();

}


@Test
public void notesListExample() throws Exception {
    mockMvc.perform(get("/api/notes/"))
           .andExpect(status().isOk())
           .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
           .andDo(document("notes-list-example", responseFields(
                   fieldWithPath("[]").description("An array of <<note-example,note>>s."))));
}

@Configuration
@EnableWebMvc
@Import(JacksonCustomizations.class)
public static class TestConfiguration {
    @Bean
    public NoteController noteController() {
        return new NoteController();
    }
}

请注意我的测试中的应用程序上下文如何导入 JacksonCustomizations 配置。

我发现的其他东西:

  • 在我的引导应用程序上添加 @EnableWebMvc 会停止自定义工作。
  • 在我的测试中删除 @EnableWebMvc 会停止生成 JSON。

最佳答案

NoteControllerDocumentation 未配置为使用 Spring Boot 创建应用程序上下文。这意味着 Spring Boot 的自动配置不会运行,因此您的自定义 Jackson 模块不会应用于 ObjectMapper

解决您问题的最简单方法是删除您的 TestConfiguration 类并更新 SpringApplicationConfiguration 以引用 DemoApplication。这将为您留下以下代码:

package com.example.controller;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.example.DemoApplication;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
@WebAppConfiguration
public class NoteControllerDocumentation {

    @Rule
    public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(context)
                                 .apply(documentationConfiguration(restDocumentation))
                                 .build();

    }

    @Test
    public void notesListExample() throws Exception {
        mockMvc.perform(get("/api/notes/"))
               .andExpect(status().isOk())
               .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
               .andExpect(content().json("[{\"id\":1}]"))
               .andDo(print())
               .andDo(document("nodes-list-example", responseFields(
                       fieldWithPath("[]").description("An array of <<note-example,note>>s."))));
    }

}

或者,如果您想更好地控制控件的创建方式(例如,注入(inject)模拟服务),您可以使用自定义配置类。关键是用 @EnableAutoConfiguration 注释该类,以便启用 Spring Boot 的自动配置并执行 ObjectMapper 的自定义。这种方法将为您留下以下代码:

package com.example.controller;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentation;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.example.JacksonCustomizations;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@WebAppConfiguration
public class NoteControllerDocumentation {

    @Rule
    public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(context)
                                 .apply(documentationConfiguration(restDocumentation))
                                 .build();

    }

    @Test
    public void notesListExample() throws Exception {
        mockMvc.perform(get("/api/notes/"))
               .andExpect(status().isOk())
               .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
               .andExpect(content().json("[{\"id\":1}]"))
               .andDo(print())
               .andDo(document("nodes-list-example", responseFields(
                       fieldWithPath("[]").description("An array of <<note-example,note>>s."))));
    }

    @Configuration
    @EnableAutoConfiguration
    @Import(JacksonCustomizations.class)
    static class TestConfiguration {

        @Bean
        public NoteController notesController() {
            return new NoteController();
        }

    }

}

关于java - Spring REST 文档测试中未使用自定义 Jackson 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34156932/

相关文章:

java - 练习考试中模棱两可的循环

java - Android Studio 中的 OnClickListener

java spring命名查询和属性文件

java - HTTP 状态 500 - 预期 session 属性 'userObject'

java - 如何模拟已注入(inject)依赖项的父类(super class)

java - 如何保存 ListView 状态并返回到该状态

java - 在Gradle中定义任务之前添加对任务的依赖关系

java - Apache cxf jaxrs 服务器 ServiceConstructionException

jquery - ajax调用中返回ModelAndView的区别

java - 在 Spring Controller 中返回未捕获异常的 JSON