Forum Discussion

1aleksandrmd1's avatar
1aleksandrmd1
Occasional Visitor
4 years ago

Running tests with javaagent using api

Hello!
I have a written, proprietary, maven plugin for running tests using cucumber. I don't use maven-surefire-plugin.
The cucumber plugin runs:

final Runtime runtime = Runtime.builder().withArgs(argv).withClassLoader(classLoader).build();
runtime.run();

 Now the task is to run tests using javaagent to use aspect. I tried two options
The first dynamic connection of the agent, using the code

public static boolean isAspectJAgentLoaded(String jarFilePath) {
             try {
                    Agent.getInstrumentation();
             } catch (NoClassDefFoundError e) {
                    return false;
             } catch (UnsupportedOperationException e) {
                    return dynamicallyLoadAspectJAgent(jarFilePath);
             }
             return true;
       }

       public static boolean dynamicallyLoadAspectJAgent(String jarFilePath) {
             String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
             try {
                    File file = new File(jarFilePath);
                    VirtualMachine vm = VirtualMachine.attach(pid);
                    vm.loadAgent(file.getAbsolutePath());
                    vm.detach();
             } catch (Exception e) {
                    return false;
             }
             // DynamicLoad.isAspectJAgentLoaded()
             // Agent.getInstrumentation();
             return true;
       }

Second option. Implemented starting a new process from a plugin, with the javaagent parameter

pb = new ProcessBuilder(javaBin, "-javaagent:" + System.getProperty("javaagent"), "-cp",
                                               createCP(project.getTestClasspathElements()), "ru.sbt.qa.ufs.start.RunMain", "-r",
                                               createR(runs)).inheritIO();


Both options did not lead to the expected result. Aspectj didn't work.
Note. When using maven-surefire-plugin, Aspectj works

<plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.18.1</version>
                        <configuration>
                            <junitArtifactName>junit:junit</junitArtifactName>
                            <testFailureIgnore>false</testFailureIgnore>
                            <skipTests>false</skipTests>
                            <argLine>
                                -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/1.9.4/aspectjweaver-1.9.4.jar
                                -Dcucumber.options="--plugin io.qameta.allure.cucumber4jvm.AllureCucumber4Jvm
                                --plugin pretty --tags test --glue ru.sbtqa.tag.stepdefs.ru --glue ru.sbtqa.ufs.stepdefinitions
                                "
                            </argLine>
                        </configuration>

y guess is that perhaps the cucumber is creating a new process. Maybe somehow it can be passed javaagent using Runtime.builder ()? Can anyone suggest a solution to this problem?

No RepliesBe the first to reply