mikamika
mikamika

Reputation: 188

AssertJ custom Aspects doesn't work with maven-surefire-plugin

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?

Upvotes: 0

Views: 467

Answers (2)

mikamika
mikamika

Reputation: 188

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.

Upvotes: 0

kriegaex
kriegaex

Reputation: 67437

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.

Upvotes: 1

Related Questions