Showing results for 
Search instead for 
Did you mean: 

Running tests with javaagent using api

Occasional Visitor

Running tests with javaagent using api

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();;

 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 {
             } 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);
             } 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()), "", "-r",

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

                                -Dcucumber.options="--plugin io.qameta.allure.cucumber4jvm.AllureCucumber4Jvm
                                --plugin pretty --tags @test --glue --glue ru.sbtqa.ufs.stepdefinitions

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?

New Here?
Join us and watch the welcome video: