Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-instrument javaagent fails to weave classes that are parameters to a junit test class method [SPR-15926] #20480

Open
spring-projects-issues opened this issue Sep 5, 2017 · 0 comments
Labels
in: core in: test status: waiting-for-triage

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Sep 5, 2017

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

@spring-projects-issues spring-projects-issues added type: bug status: waiting-for-triage in: test in: core and removed type: bug labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core in: test status: waiting-for-triage
Projects
None yet
Development

No branches or pull requests

1 participant