Search code examples
javaspringjunitjavaagents

How to integrate Spring Instrument javaagent in ALL JUnit tests


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!


Solution

  • 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