首页 > 解决方案 > Spring 5 MVC Test with MockMvc, test-context.xml, and annotation-based WebAppConfig (ie, in Java)

问题描述

版本(涉及 SpringBoot):

Spring: 5.2.16
web-app / servlet API: 4.0
JUnit: 5.8

Spring MVC 测试不适用于返回的控制器端点ResponseEntity<ReturnStatus>,其中ReturnStatus是具有适当 getter/setter 的 POJO。触发的异常表明 JSON 转换不适用于ReturnStatus. 我的研究表明未加载 WebApplicationContext 的基于注释的 Java 配置(因此无法识别 Jackson JSON 转换器)。奇怪的是,在 Tomcat 的非测试部署中,控制器端点工作正常,可能是因为web.xmlTomcat 解析了 war 文件中的 。

问题:
如何调整此应用程序的 Spring MVC 测试设置,以便正确加载 WebApplicationContext 的基于注释的 Java 配置?例如,这可以在端点测试逻辑(即 JUnit 测试)中显式完成吗?

例外:

14:33:57,765  WARN DefaultHandlerExceptionResolver:199 - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.acme.myapp.io.ReturnStatus] with preset Content-Type 'null']
14:33:57,765 DEBUG TestDispatcherServlet:1131 - Completed 500 INTERNAL_SERVER_ERROR

Spring MVC 应用程序包含以下配置:

  1. test-context.xml,其中包含用于访问数据存储的 Spring bean 配置:
  2. web.xml,它声明并映射了DispatcherServletWebApplicationContext 的相关设置。
  3. .java 实现中基于注解的配置WebMvcConfigurer

相关摘录test-context.xml

  <context:component-scan base-package="com.acme.myapp"/>
  <jpa:repositories base-package="com.acme.myapp.repos"/>

  <context:property-placeholder location="classpath:/application.properties" />

  <!-- Data persistence configuration -->
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>
  <tx:annotation-driven transaction-manager="transactionManager" />

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="${db.showSql}" />
        <property name="databasePlatform" value="${db.dialect}" />
        <property name="generateDdl" value="${db.generateDdl}" />
      </bean>
    </property>
    <property name="packagesToScan">
      <list>
        <value>com.acme.myapp.dao</value>
      </list>
    </property>
  </bean>

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.user}" />
    <property name="password" value="${db.pass}" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />
    <property name="accessToUnderlyingConnectionAllowed" value="true"/>
  </bean>

  <!-- Set JVM system properties here. We do this principally for hibernate logging. -->
  <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject">
      <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="java.lang.System" />
        <property name="targetMethod" value="getProperties" />
      </bean>
    </property>
    <property name="targetMethod" value="putAll" />
    <property name="arguments">
      <util:properties>
        <prop key="org.jboss.logging.provider">slf4j</prop>
      </util:properties>
    </property>
  </bean>

相关摘录web.xmlapplication-context.xml我们的生产版本在哪里test-context.xml):

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-context.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>central-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
      <param-name>contextClass</param-name>
      <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.acme.myapp.MyAppWebAppConfig</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>central-dispatcher</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

摘自 Java 实现WebMvcConfigurer(即,我们合并了 Jackson JSON 转换器):

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "com.acme.myapp.controllers" })
public class MyAppWebAppConfig implements WebMvcConfigurer
{
  private static final Logger logger = LoggerFactory.getLogger(MyAppWebAppConfig.class);

  @Override
  public void extendMessageConverters(List<HttpMessageConverter<?>> converters)
  {
    logger.debug("extendMessageConverters ...");
    converters.add(new StringHttpMessageConverter());
    converters.add(new MappingJackson2HttpMessageConverter(new MyAppObjectMapper()));
  }
}

控制器端点如下所示(根位于/patients):

  @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<ReturnStatus> readPatient(
    @PathVariable("id") long id
  )
  {
    ReturnStatus returnStatus = new ReturnStatus();
    returnStatus.setVersionId("1.0");
    ...
    return new ResponseEntity<ReturnStatus>(returnStatus, httpStatus);
  }

使用 JUnit5 和 MockMvc,端点测试看起来像这样:

@SpringJUnitWebConfig(locations={"classpath:test-context.xml"})
public class PatientControllerTest
{
  private MockMvc mockMvc;

  @BeforeEach
  public void setup(WebApplicationContext wac) {
    this.mockMvc = webAppContextSetup(wac).build();
  }

  @Test
  @DisplayName("Read Patient from /patients API.")
  public void testReadPatient()
  {
    try {
      mockMvc.perform(get("/patients/1").accept(MediaType.APPLICATION_JSON_VALUE))
        .andDo(print())
        .andExpect(status().isOk());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

谢谢!

标签: spring-mvcjunit5mockmvcspring-mvc-test

解决方案


以下是一些选项,可能并不详尽:

  • 根据前面的评论,我们可以简单地<mvc:annotation-driven>test-context.xml. 例如:
  <bean id="myappObjectMapper" class="com.acme.myapp.MyAppObjectMapper"/>
  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <constructor-arg ref="myappObjectMapper"/>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>

实际上,该指令消除了对 loading 的需要MyAppWebAppConfig<mvc:annotation-driven>事实上它是@EnableWebMvcJava 中注解的 XML 等价物。

  • 实现WebApplicationInitializer以便在 Java 中有效地完成我们配置的web.xml. 例如:
public class MyAppWebApplicationInitializer implements WebApplicationInitializer
{
  @Override
  public void onStartup(ServletContext container)
  {
    XmlWebApplicationContext appCtx = new XmlWebApplicationContext();
    appCtx.setConfigLocation("classpath:application-context.xml");
    container.addListener(new ContextLoaderListener(appCtx));

    AnnotationConfigWebApplicationContext dispatcherCtx = new AnnotationConfigWebApplicationContext();
    dispatcherCtx.register(MyAppWebAppConfig.class);

    ServletRegistration.Dynamic registration = container.addServlet("central-dispatcher", new DispatcherServlet(dispatcherCtx));
    registration.setLoadOnStartup(1);
    registration.addMapping("/api/*");
  }  
}

对于这个解决方案,我们web.xml从项目中删除;可能我们也应该参数化对的引用application-context.xml

请注意,当我运行 JUnit5 测试时,Spring似乎没有instance MyAppWebApplicationInitializer,相反,为 JUnit5 加载的 Spring 上下文是@SpringJUnitWebConfig注释引用的上下文。因此,我建议将与测试相关的配置与test-context.xml,并保留WebApplicationInitializer用于生产。

我敢肯定还有其他选择,但我只探讨了这两种方法。


推荐阅读