Aspectj class is not found by test class when running test with maven

佐手、 提交于 2021-01-29 09:44:01

问题


I have created a test for my aspectj class.

When I execute my test it works fine to "Run as TestNG" from Eclipse.

Then when I execute it in maven:

mvn clean test

I get the following error:

[15:15] [eraonel/git/java-runtime-stats] -> mvn clean test
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building java-runtime-stats 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ java-runtime-stats ---
[INFO] Deleting /repo/eraonel/git/java-runtime-stats/target
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (default) @ java-runtime-stats ---
[INFO] Executing tasks

main:
     [echo] BUILDING : /repo/eraonel/git/java-runtime-stats/src/main/java/com/company/commonlibrary/javaruntimestats/Version.java
     [echo] BUILD 2018-10-24 13:18 UTC : /repo/eraonel/git/java-runtime-stats/src/main/java/com/company/commonlibrary/javaruntimestats/Version.java
[INFO] Executed tasks
[INFO]
[INFO] --- maven-java-formatter-plugin:0.6.1-threadsafe:format (default) @ java-runtime-stats ---
[INFO] Using 'UTF-8' encoding to format source files.
[INFO] Number of files to be formatted: 21
[INFO] Successfully formatted: 1 file(s)
[INFO] Fail to format        : 0 file(s)
[INFO] Skipped               : 20 file(s)
[INFO] Approximate time taken: 0s
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-runtime-stats ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /repo/eraonel/git/java-runtime-stats/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ java-runtime-stats ---
[INFO] Compiling 15 source files to /repo/eraonel/git/java-runtime-stats/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-runtime-stats ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 11 resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ java-runtime-stats ---
[INFO] Compiling 6 source files to /repo/eraonel/git/java-runtime-stats/target/test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /repo/eraonel/git/java-runtime-stats/src/test/java/com/company/commonlibrary/javaruntimestats/aspects/DeprecatedMethodsAspectTest.java:[17,13] cannot find symbol
  symbol:   class DeprecatedMethodsAspect
  location: class com.company.commonlibrary.javaruntimestats.aspects.DeprecatedMethodsAspectTest
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.127 s
[INFO] Finished at: 2018-10-24T15:18:08+02:00
[INFO] Final Memory: 33M/730M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project java-runtime-stats: Compilation failure
[ERROR] /repo/eraonel/git/java-runtime-stats/src/test/java/com/company/commonlibrary/javaruntimestats/aspects/DeprecatedMethodsAspectTest.java:[17,13] cannot find symbol
[ERROR] symbol:   class DeprecatedMethodsAspect
[ERROR] location: class com.company.commonlibrary.javaruntimestats.aspects.DeprecatedMethodsAspectTest
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

What am I missing here? Why is the aspectj not compiled before the test case? Is there a way to change this?

I used this example ( for the library part) to follow since I am creating a lib that should be used for other applications:

AspectJ: How to weave an aspect library into a Java project

TestClass:

/**
 * Unit test to see if pointcut works as expected in ${@link DeprecatedMethodsAspect}
 */
public class DeprecatedMethodsAspectTest {

    private DeprecatedMethodsAspect aspect;
    private DeprecatedMethods deprecatedMethodsMock;
    private DeprecatedMethodsApp app;

    @BeforeClass
    public void setUp() throws Exception {
        app = new DeprecatedMethodsApp();
        deprecatedMethodsMock = mock(DeprecatedMethods.class);
        when(deprecatedMethodsMock.isActive()).thenReturn(true);
        aspect = Aspects.aspectOf(DeprecatedMethodsAspect.class);
        aspect.setDeprecatedMethods(deprecatedMethodsMock);

    }

    @Test
    public void testSumIsMatched() throws Throwable {
        app.sum(1, 2);
        verify(deprecatedMethodsMock, times(1)).collect(any(JoinPoint.class));

    }

    @Test(description = " we should not gather information from methods annotated @Beta.")
    public void testSubIsNotMatched() throws Throwable {
        app.sub(2, 1);
        verify(deprecatedMethodsMock, times(0)).collect(any(JoinPoint.class));

    }

}

This is excerpt from my pom.xml

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Compile scope dependencies -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>${java.version}</version>
                    <configuration>
                        <showWeaveInfo>true</showWeaveInfo>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.version}</complianceLevel>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <!--<verbose>true</verbose> -->
                        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn> -->
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

回答1:


You've included the aspectj-maven-plugin in <pluginManagement/> but you haven't included it in <plugins>

Try adding:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
    </plugin>
</plugins>

under the <build/> element.

See also: Maven: What is pluginManagement?



来源:https://stackoverflow.com/questions/52986728/aspectj-class-is-not-found-by-test-class-when-running-test-with-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!