如何在所有 JUnit 测试中集成 Spring Instrument javaagent

Mar*_*tin 6 java junit spring javaagents

我正在为一个大型项目编写单元测试,我需要将 JVM 参数传递给它,这些是我的 JVM 参数内置到该项目的 Eclipse 运行配置中:

--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
Run Code Online (Sandbox Code Playgroud)

我的问题是我需要为每个 JUnit 测试或测试序列添加这些参数。有没有更好的方法呢?无需手动将这些参数添加到我创建的每个新测试中的某种方法?

******编辑******

这也有一个令人讨厌的副作用,即根本不让我构建这个项目!Maven 不使用我的自定义 JUnit 运行配置来运行应用程序的整个测试集(这很好用,因为我在那里设置了 JVM 参数),而是使用它自己的,因为参数不存在,这显然失败了。这是一个大问题,有没有办法以某种方式将这些 JVM 参数直接“硬编码”到 POM 中?

****** 编辑 2 ******

这是我的 POM.xml 文件中的 Spring-Boot-Maven-Plugin 配置:

<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>
Run Code Online (Sandbox Code Playgroud)

******解决方案******

添加 Maven Surefire 插件并以这种方式设置它解决了这个问题:

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢!

use*_*814 5

您可以在surefire插件中设置jvm args。使用 mvn test 运行测试。就像是

<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> 
Run Code Online (Sandbox Code Playgroud)

更多在这里http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine