Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 509
Martin Avatar asked Feb 10 '20 15:02

Martin


People also ask

Where do I put Javaagent?

To pass the -javaagent argument on WebSphere: From the admin console, select Servers > Application servers > (select a server) > Configuration > Service Infrastructure > Java and Process Management. Select Process Definition > Additional Properties, then select Java Virtual Machine. Select Apply, then select Save.

What is Javaagent option?

The -javaagent option may be used multiple times on the same command-line, thus starting multiple agents. The premain methods will be called in the order that the agents are specified on the command line. More than one agent may use the same <jarpath> .

How do I create a Javaagent?

To create a successful javaagent we'll need four things: an agent class, some meta-information to tell the JVM what capabilities to give to our agent class, a way to make the JVM load the . jar with the agent before it starts minding the application's business and a coffee. Got the coffee already?

What is Spring instrument?

The spring-instrument module provides class instrumentation support and classloader implementations to be used in certain application servers. The spring-instrument-tomcat module contains Spring's instrumentation agent for Tomcat.


1 Answers

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

like image 55
s7vr Avatar answered Oct 17 '22 22:10

s7vr