How to integrate Spring Instrument javaagent in ALL JUnit tests
Asked Answered
A

1

6

I am writing unit tests on a large project which I need to pass JVM arguments to, those are my JVM arguments built into the Eclipse run configuration for that project :

--module-path lib/javafx-sdk-13.0.2/lib --add-modules=javafx.controls
-javaagent:lib/aspectjweaver-1.9.5.jar 
-javaagent:lib/spring-instrument-5.2.3.RELEASE.jar

My issue is that I need to add those arguments for EVERY JUnit test or testing sequence. Is there a better approach for this? Some way to not have to add those arguments manually into every new test I create?

******EDIT******

This also has the nasty side-effect of not letting me build this project at all! Maven does not use my custom JUnit run config for running the entire set of tests for the application (which works fine because I set the JVM arguments in there) but rather its own which obviously fails because the arguments are not there. That is a huge problem, is there a way to "hardcode" those JVM arguments directly into the POM somehow?

******EDIT 2******

This is my Spring-Boot-Maven-Plugin config in my POM.xml file :

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <jvmArguments>
            --module-path lib/javafx-sdk-13.0.2/lib 
            --add-modules=javafx.controls
            -javaagent:lib/aspectjweaver-1.9.5.jar 
            -javaagent:lib/spring-instrument-5.2.3.RELEASE.jar
        </jvmArguments>
    </configuration>
</plugin>

******SOLUTION******

Adding the Maven Surefire plugin and setting it up this way fixed the issue :

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <argLine>
                    --module-path lib/javafx-sdk-13.0.2/lib 
                    --add-modules=javafx.controls
                    -javaagent:lib/aspectjweaver-1.9.5.jar 
                    -javaagent:lib/spring-instrument-5.2.3.RELEASE.jar
                </argLine>
            </configuration>
          </plugin>

Thanks!

Agoraphobia answered 10/2, 2020 at 15:30 Comment(0)
H
6

You can set the jvm args in the surefire plugin. Use mvn test to run tests. Something like

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>
          <argLine>-Djava.security.policy=${basedir}/src/test/resources/java.policy</argLine>
        </configuration>
      </plugin>
</plugins> 

More here http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine

Harveyharvie answered 12/2, 2020 at 19:51 Comment(8)
The only plugin currently in my POM that is not docker or launch4j is the Spring-Boot-Maven-Plugin. Would adding the surefire on top of it have any ill effects? Can the Spring-Boot plugin be setup similarly?Agoraphobia
how are you running your tests ? mvn command ? You can add jvm args to spring boot maven plugin too. Use jvmArguments inside configuration attributeHarveyharvie
My run configuration for the maven build of this project contains the clean, install and package goals. That is what I use to build it.Agoraphobia
No go, still getting "ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method." error which is a sign Spring-Instrument is not loaded. I will add my Spring-Boot plugin config to my original post.Agoraphobia
I dont think spring boot plugin is correct for running tests. You should add the surefire plugin and tests should be executed as part of test phase. What plugin is associated with test phase right now ? Are you running them using eclipse maven plugin ?Harveyharvie
I'm afraid I'm not as fluent with Maven as I should be, not sure I can answer this question. Honestly all I can tell you is that my maven build command executes the clean, install and package goals and the only plugin that is not Launch4J or Docker (which I got to work perfectly) seems to run during the repackage goal (if there is even such thing), as you see in my original post.Agoraphobia
I believe I got it to work based on your suggestions and after exploring a bit. hang tight hahahaAgoraphobia
That worked! I will update my original post, thank you very much! Does this mean that there would be a way to eliminate the need to add these arguments in the java -jar command that is run to start the program outside the IDE using the POM too?Agoraphobia

© 2022 - 2024 — McMap. All rights reserved.