【问题标题】:Spring doesn't autowire @Configurable when running Dropwizard integration test during maven build在 Maven 构建期间运行 Dropwizard 集成测试时,Spring 不会自动装配 @Configurable
【发布时间】:2015-09-21 07:45:21
【问题描述】:

我正在使用 Spring 的 @Configurable 在 Dropwizard 应用程序中自动装配使用“new”构造的 bean。我有一个集成测试,它使用 DropwizardAppRule 来启动应用程序,并使用 aspectj-maven-plugin 进行编译时编织。

当我从 IDEA 构建并运行集成测试时,bean 已按预期连接并且测试通过。

当我运行“mvn clean install”时,bean 未连接,测试失败并出现 NullPointerException。

当我运行“mvn clean install -DskipTests”并启动应用程序时,bean 连接正确。

我的问题是为什么在“mvn clean install”期间它会失败?

aspectj-maven-plugin 在流程源阶段运行,因此应在集成测试运行之前检测类:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.8</version>
    <configuration>
      <complianceLevel>1.8</complianceLevel>
      <source>1.8</source>
      <target>1.8</target>
      <aspectLibraries>
        <aspectLibrary>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aspects</artifactId>
        </aspectLibrary>
      </aspectLibraries>
    </configuration>
    <executions>
      <execution>
        <phase>process-sources</phase>
        <goals>
          <goal>compile</goal>
          <goal>test-compile</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

如果我反编译该类,我可以看到它确实已被检测。

如果我在 @Autowired 设置器中设置断点并从 IDEA 运行集成测试,我可以看到该类正在由 Spring 连接。

运行 'mvn clean install' 时,它根本不会中断设置器。

用@Resource 替换@Autowired 没有帮助。

我有一个带有 @EnableSpringConfigured 的 Spring 配置类。我最好的猜测是 DropwizardAppRule 没有使用正确的 Spring 配置,尽管其他 Spring 组件正在正确管理。

非常感谢任何帮助。谢谢。

编辑

我还测试了默认的surefire(maven 3.2.5)和:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
</plugin>

【问题讨论】:

    标签: spring aspectj dropwizard surefire aspectj-maven-plugin


    【解决方案1】:

    我想通了,但需要更多的上下文来解释这个问题。 @Configurable 在枚举中被实例化,如下所示:

    public enum Day {
        MONDAY(new MyConfigurableObject()),
        ...
    }
    

    单元测试和集成测试一起运行,单元测试在 spring 上下文可用之前实例化枚举。由于枚举存在于静态上下文中,因此集成测试随后使用了未连接的枚举。

    解决方案是将单元测试和集成测试分开执行。我使用 maven-failsafe-plugin 这样做:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
            <excludes>
                <exclude>it/**</exclude>
            </excludes>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
            <includes>
                <include>it/**</include>
            </includes>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 2021-10-15
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多