【问题标题】:AspectJ not weaving on test-compile but weaves on compile. Therefore unit tests throw ClassNotFoundException when running with mavenAspectJ 不是在测试编译时编织,而是在编译时编织。因此单元测试在使用 maven 运行时会抛出 ClassNotFoundException
【发布时间】:2011-07-07 13:46:19
【问题描述】:

我所有的 junit 测试都在 eclipse 中通过,但是当从控制台运行时,它们似乎为 AJ 类 RiskLogAspect.aj 抛出 ClassNotFoundException 我可以验证在 eclipse 中这是正确的编织并提供了所需的功能。

我尝试将依赖项添加到我的项目中以确保所有内容都正确。

我开始添加 aspectj-maven-plugin

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.3</version>
        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                    <goal>test-compile</goal>
                </goals>
            <phase>process-sources</phase>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <encoding>UTF8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                    <aspects>
                        <includes>
                            <include>com.idna.riskprofile.RiskLogAspect</include>
                        </includes>
                    </aspects>
                </configuration>
            </execution>
        </executions>
    </plugin>

然后我添加了以下依赖项

      <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.7</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.7</version>
    </dependency>

有什么想法吗?

我正在运行 maven 2.0.9 和 jdK 1.6 update 21

在更改我的一些配置后,我注意到控制台中出现了以下“[Xlint:adviceDidNotMatch]”,它似乎正在学习我的课程,但没有正确应用切入点。

对于目标编译,它找到 AJ 文件并正确应用建议,对于测试编译目标,它说 Xlint:adviceDidNotMatch 并在一半的 junit 测试中抛出类未找到异常。 从控制台:

[INFO] [aspectj:compile {execution: default}]
[INFO] Join point 'method-execution(java.lang.String com.idna.riskprofile.impl.RiskProfileEntryPointImpl.execute(com.idna.riskprofile.domain.RiskProfileRequest))' in Type 'com.idna.riskprofile.impl.RiskProfileEntryPointImpl' (RiskProfileEntryPointImpl.java:35) advised by around advice from 'com.idna.riskprofile.logging.aspects.RiskLogAspect' (RiskLogAspect.aj:35)
[INFO] [aspectj:test-compile {execution: default}]
[WARNING] advice defined in com.idna.riskprofile.logging.aspects.RiskLogAspect has not been applied [Xlint:adviceDidNotMatch]
[INFO] [resources:resources]

更新:似乎没有在测试编译目标上编织建议并不是导致控制台上测试失败的原因,而是 spring 配置的另一个问题。

【问题讨论】:

  • 当你说顶级包时 - 你是指源包还是父 maven 模块。您能否发布整个 pom 或至少相关部分
  • 您是否使用 Maven...在 Eclipse 中更新项目配置?
  • 我已尝试更新项目配置。该包也是 com.myproject.somepackage.X 并且所有内容都位于 somepackage 下面,所以我删除了它。

标签: java maven junit aspectj


【解决方案1】:

注册aspectj插件还不够,还需要注册一些目标:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <!-- use this goal to weave all your main classes -->
                <goal>test-compile</goal>
                <!-- use this goal to weave all your test classes -->
            </goals>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 好的,我试过了。没什么区别。我最初遇到很多问题,表明我需要使用 JDK 1.5 或更高版本(我使用的是 1.6),所以我将配置添加到使用 1.6 的目标中。结束在我开始的同一个地方。测试在 Eclipse 中通过,但在控制台上没有通过。
猜你喜欢
  • 1970-01-01
  • 2013-03-18
  • 2012-11-21
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多