Ken Wang opened SPR-15926 and commented
I have aspectj set up in my spring application, using the spring-instrument javaagent to run my junit test through maven.
Here is some sample code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20</version>
<configuration>
<argLine>-javaagent:/path/to/spring-instrument.jar</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
@Configurable
public class ConfigurableEntity {
@Autowired
private SomeBean someBean;
private boolean isAutowired() {
return this.someBean != null;
}
}
<context:load-time-weaver/>
<context:annotation-config/>
<context:spring-configured/>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("test-context.xml")
public class ConfigurableTest {
@Test
public void testConfigurable() {
assertTrue(new ConfigurableEntity().isAutowired());
}
}
The above code does work. However, the following does not:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("test-context.xml")
public class ConfigurableTest {
@Test
public void testConfigurable() {
assertTrue(new ConfigurableEntity().isAutowired());
}
public void doSomething(ConfigurableEntity entity) {
...
}
}
The problem is that prior to running the test, the maven surefire plugin makes a call to ConfigurableTest.getDeclaredMethods(), which at that point will load class ConfigurableEntity. However, spring has not yet had a chance to enable the loadTimeWeaver yet, and so ConfigurableEntity is unwoven and the above test fails.
I have found some workarounds to the above issue... but I am wondering if this is a known problem. Since this is just a unit test, it isn't too major of a concern, but it worries me that there might be instances in my main application where an advised class is loaded before spring has a chance to enable weaving.
Affects: 4.3.8
Comment From: snicoll
The problem is that prior to running the test, the maven surefire plugin makes a call to ConfigurableTest.getDeclaredMethods(), which at that point will load class ConfigurableEntity. However, spring has not yet had a chance to enable the loadTimeWeaver yet,
I don't think that's accurate. This works via an agent added to the JVM as you're aware given the configuration so Spring has no control over when this happens. If the agent is configured, then it shouldn't matter what surefire does or does not do. Using an agent like this also means that the process has to be forked.
@sbrannen was wondering if perhaps you had the failsafe plugin configured, but not surefire and the problem was with the regular test phase.