Spring @Autowire does not find beans through `mvn verify`

Viewed 581

I have a Spring (not Spring Boot) project at https://github.com/poggs/spring-autowire-it-problem.

When running the integration test ExampleComponentIT through IntelliJ IDEA, both tests pass and the code can find the bean ExampleComponent. When running the integration test with mvn verify, it fails as it can't find ExampleComponent.

What I want is for mvn verify to be able to find ExampleComponent as it's executed as part of the CI build process.

Can anyone point out where I'm going wrong?

3 Answers

Peter, I have fixed this problem by a temporal workaround

mvn verify -Dfailsafe.useModulePath=false

The fix is already done and it will be released in the version 3.0.0-M6. There the workaround useModulePath=false would not be needed anymore.

It does not make sense to name it an integration test, e.g. ExampleComponentIT, if you want to load the classes from target/classes.

The version 3.0.0-M5 is right, and you should properly build or guarantee that the JAR file works as expected and then maven-failsafe-plugin would work properly with the integration tests.

The difference between Surefire and Failsafe is that Failsafe may fail on verify but the Surefire fails on test phase. Additionally, the Failsafe plugin uses JAR file instead of target/classes which is what the integration test expect due to the package phase happens before the phases integration-test and verify.

When running maven-surefire-plugin, specifically 3.0.0-M5, through mvn clean verify, the directory target/classes is not on the classpath. This directory contains the compiled Java code needed by the integration test.

Adding the following to the maven-failsafe-plugin configuration in pom.xml fixes the problem:

<configuration>
  <additionalClasspathElements>                     
    <additionalClasspathElement>target/classes</additionalClasspathElement>
  </additionalClasspathElements>
</configuration>

Alternatively, downgrading to 3.0.0-M4 works.

Related