0

I have the maven project written on Kotlin.

pom.xml uses maven-surefire-plugin plugin:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteName>${suiteName}</suiteName>
                    </suiteXmlFiles>
                    <testFailureIgnore>false</testFailureIgnore>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <systemPropertyVariables>
                        <env>${env}</env>
                        <deviceName>${deviceName}</deviceName>
                    </systemPropertyVariables>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

I'd like to add additional aspect to my code, so I added another plugin:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <verbose>true</verbose>
                    <Xlint>ignore</Xlint>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <forceAjcCompile>true</forceAjcCompile>
                    <weaveDirectories>
                        <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                        <weaveDirectory>${project.build.testOutputDirectory}</weaveDirectory>
                    </weaveDirectories>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

with aspectjrt dependency.

In this state, my tests are working.

Next, I created a new class with @Aspect annotation - still working.

Now I am adding very simple code (just to check) in this class:

@Pointcut("execution(* *(..))")
fun method() {
}

@AfterReturning(pointcut = "method()")
fun afterMethod() {
    println("After method")
}

This causes suite to fail with:

[ERROR] There was an error in the forked process
[ERROR] 'utils.Aspects utils.Aspects.aspectOf()'
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] 'utils.Aspects utils.Aspects.aspectOf()'
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:656)
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:282)
[ERROR]     at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:245)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1183)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR]     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:301)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:211)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:165)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:157)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:121)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:127)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:294)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR]     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR]     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:960)
[ERROR]     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293)
[ERROR]     at org.apache.maven.cli.MavenCli.main(MavenCli.java:196)
[ERROR]     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
[ERROR]     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR]     at java.base/java.lang.reflect.Method.invoke(Method.java:564)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)

I have created stand-alone project to check if understand AOP concept correctly here: https://github.com/heavy-razzer/AssertJ-Maven-Kotlin It work.

So looks like there is something wrong with the configuration of my main project, but what?

mikamika
  • 110
  • 1
  • 6

2 Answers2

1

I just cloned your sample project, which was verfy helpful. For me, the project works just fine in Maven:

[INFO] --- aspectj-maven-plugin:1.14.0:test-compile (default) @ aop-project-kotlin ---
(...)
[INFO] Join point 'method-execution(void tests.NewTest.myTest())' in Type 'tests.NewTest' (NewTest.kt:12) advised by before advice from 'Aspects' (test-classes!Aspects.class(from Aspects.kt))
[INFO] Join point 'method-execution(void steps.Steps.printCaption())' in Type 'steps.Steps' (Steps.kt:7) advised by afterReturning advice from 'Aspects' (test-classes!Aspects.class(from Aspects.kt))
[INFO] Join point 'method-execution(void steps.Steps.printMessage())' in Type 'steps.Steps' (Steps.kt:11) advised by afterReturning advice from 'Aspects' (test-classes!Aspects.class(from Aspects.kt))
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ aop-project-kotlin ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running tests.NewTest
Test started: Very simple test
This this a simple test
Test step performed: printCaption
Testing in progress
Test step performed: printMessage
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.592 s - in tests.NewTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

In IntelliJ IDEA, it does not work out of the box, because doing post-compile weaving with Maven in a single module together with Kotlin compilation does not work out of the box. The test runs, but the aspect is not triggered. You need to delegate the build and run actions to Maven to be able to run the test directly from IDEA:

IntelliJ IDEA Maven runner settings

Test started: Very simple test
This this a simple test
Test step performed: printCaption
Testing in progress
Test step performed: printMessage

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Under no circumstances, however, can I reproduce your Maven problem.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Yep, sample project works fine. The problem is another problem, that is way more complex. – mikamika Aug 30 '22 at 10:48
  • May I ask why you waste other people's time by sharing a reproducer which reproduces nothing? I spent time to clone, compile, investigate both Maven and IntelliJ and finally write a comprehensive answer. I might have been able to help with your **real** problem, too, if you had shared an actual reproducer for it. Now I am feeling like an April's Fool who just got toyed with. – kriegaex Aug 30 '22 at 13:39
  • Really sorry if i made you feel that. I really appreciate your help here. That public GH project is very simple and of course, works. I created it just to check if I set AspectJ correctly before enabling it in a bigger real project. I can't share its code here, so in the initial message, I described what I have done, from maven-surefire-plugin to adding my own Aspects class. In my answer, I described workaround to fix the issue partially. But still I have a problem when there is one library (Qase) that uses Aspects, and my own Aspects, it doesn't work together. – mikamika Aug 31 '22 at 19:27
  • I run project with maven through the command line (but thanks for the idea about Idea runner config). – mikamika Aug 31 '22 at 19:30
  • 1
    [MCVE](https://stackoverflow.com/help/mcve) != your original code. You can start with the original project, strip off as much as possible, until you have the absolute minimum which still reproduces the problem. Then you rename the remaining packages, classes and methods in order to obfuscate the copyright-protected or confidential code. Every person calling themselves developers ought to be able to do that. – kriegaex Sep 01 '22 at 08:32
0

The problem in my main project (not that sample project I shared here) lies in qase-testng library that I use to upload test run results to the Qase Test Management system.

  1. me and Qase use AssertJ and have aspects logic implemented in some classes
  2. Qase requires some additional configuration to SureFire maven plugin to upload all test data correctly.
  3. SureFire uses AssertJ too

And when I run my project, SureFire got exceptions when trying to combine Aspects from my code and from Qase library.

Workaround is:

  1. Remove argLine from SureFire config (from Qase testing docs)
  2. Remove goal "compile" from aspectj-maven-plugin config - it is not needed for a project with tests.

The only drawback of these actions is that step names in the Qase dashboard will not have green/red icons next to them in the test run page. I have contacted Qase support, and they are thinking about this situation.

mikamika
  • 110
  • 1
  • 6