gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:57:03 25 4
gpt4 key购买 nike

我正在开发一个 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/

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