java - 我可以配置 MockMvcBuilders.standaloneSetup() 以使用我的消息转换器 XML 配置吗?

标签 java spring spring-mvc spring-test spring-test-mvc

我正在开发一个 Spring MVC 3.2 Web 应用程序,我正在尝试使用新的 MockMvc在我的单元测试中测试实用程序。我想测试单个 Controller 和整个 Web 应用程序加载我的 Spring 配置 XML 文件。从 Javadoc 看来,我想使用 standaloneSetup对于前者和 webAppContextSetup对于后者。

但是,当我的 Controller 寻找消息转换器来转换输出时,我遇到了问题。我对 standaloneSetup 方法的 Javadoc 的解释似乎不正确。这是此方法的 Javadoc:

Build a MockMvc by registering one or more @Controller's instances and configuring Spring MVC infrastructure programmatically. This allows full control over the instantiation and initialization of controllers, and their dependencies, similar to plain unit tests while also making it possible to test one controller at a time.

When this option is used, the minimum infrastructure required by the DispatcherServlet to serve requests with annotated controllers is automatically created, and can be customized, resulting in configuration that is equivalent to what the MVC Java configuration provides except using builder style methods.

If the Spring MVC configuration of an application is relatively straight-forward, for example when using the MVC namespace or the MVC Java config, then using this builder might be a good option for testing a majority of controllers. A much smaller number of tests can be used to focus on testing and verifying the actual Spring MVC configuration.

我将“最小基础设施”和“及其依赖项”解释为意味着除了它们的所有依赖项之外我指定的 Controller 将被加载,尽管我包括消息转换器。然而,情况似乎并非如此。我需要针对 2 个要求的自定义配置:

  1. 使用“application/json”的 PUT 操作被转换为带有 Joda DateTime 字段的 POJO。因此,我在我的 Spring 配置中包含以下内容:

    <mvc:annotation-driven>
      <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
           <property name="objectMapper" ref="myObjectMapper" />
        </bean>
      </mvc:message-converters>
    </mvc:annotation-driven>
    

    这适用于 webAppContextSetup 但如果我想使用 standaloneSetup 看起来我需要手动创建和配置一个 MappingJackson2HttpMessageConverter 配置我的自定义 ObjectMapper 注册了 JodaModule

    standaloneSetup(myController).setMessageConverters(myJsonConverter).build();
    
  2. 生成“application/custom+xml”的 GET 操作。当我加载 webAppContextSetup 时,无需额外配置即可运行。但是,当我加载 standaloneSetup 时,之前发生的任何 Spring 配置都没有发生,我看到以下错误消息:

    org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    

    有人可以描述这里发生的事情吗?当我调用 webAppContextSetup 时是否包含一些隐藏的或默认的消息转换器,但是当我调用上面的代码时,我是否以某种方式覆盖了那些转换器?我怎样才能包括他们?

MockMvc 是否有更简单的方法来设置单个 Controller 和 mvc:annotation-driven 中的所有配置?我可以将 MockMvcBuilders.standaloneSetup() 配置为使用我的消息转换器 XML 配置而不是手动配置每个配置吗?

最佳答案

我认为没有更简单的方法。您必须通过 StandaloneMockMvcBuilder#setMessageConverters 手动注册每个转换器.这是有道理的,因为您在 XML 配置中执行相同的操作。请注意,如果使用 Java @Configuration 也是一样的类。

独立设置旨在加载尽可能少的组件。在 Spring 测试术语中,它最接近单元测试。如果它是您想要的单元测试,您应该使用类似 Mockito 的模拟框架模拟您要测试的 Controller 的每个依赖项(包括转换器)。 .

你可以找到一个例子here (这是为大学编写的教学项目)。

关于java - 我可以配置 MockMvcBuilders.standaloneSetup() 以使用我的消息转换器 XML 配置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18254669/

相关文章:

java - springboot 升级后 @JsonProperty 不工作

ajax - Spring MVC Controller 抛出 415 错误媒体不支持 JSON 请求

spring - 使用 MockMultipartFile 测试最大上传文件大小

java - 创建您自己的 Swing 组件

java - 添加validation-api-2.0.1.final.jar会出现错误

java - 多 Maven 模块项目中的 Spring MVC

java - jackson 更改时间戳格式

Spring MVC 在 intellij idea 10 中不起作用

java - String.format 无法在某些设备上格式化数字?

java - 如何在 Spring Boot 中将 S3 对象转换为资源?