在maven build

时间:2015-09-21 07:45:22

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

我使用Spring的@Configurable来自动装配一个由新的'构建的bean。在Dropwizard应用程序中。我有一个集成测试,它使用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 setter中设置断点并从IDEA运行集成测试,我可以看到该类正在由Spring连接。

在运行&#39; mvn clean install&#39;时,它根本不会在设置器中中断。

用@Resource取代@Autowired并没有帮助。

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

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

修改

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

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

1 个答案:

答案 0 :(得分:0)

我想出来了,但需要更多的背景来解释这个问题。 @Configurable正在枚举中实例化:

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

单元和集成测试一起运行,单元测试在弹簧上下文可用之前实例化枚举。由于枚举存在于静态上下文中,因此集成测试会使用无线枚举。

解决方案是拆分单元和集成测试执行。我是这样使用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>