junit中的模拟方面无法应用

时间:2013-12-16 09:05:30

标签: maven junit aspectj

我正在构建一个简单的项目来学习aspectj。

这是来自第2行动的方面,这个想法非常简单---- MessageCommunicator将负责传递消息,它是主要的业务逻辑。同时,Authenticator将负责身份验证,并将按照声明的SecurityAspect进行编织。

虽然在日志中看到该方面正在运行非常简单。我仍然希望确保它在junit情况下有效。

在我的项目中,我使用maven 3.0.4 with aspectj 1.7.3和aspect-maven-plugin 1.5。

现在问题是在编译测试用例时出现警告。因此,测试包中的方面不起作用。但是,如果您在源包中编写Main类并运行,则源包中的方面将起作用。

构建时的警告消息:

[INFO] --- aspectj-maven-plugin:1.5:test-compile (test-compile_with_aspectj) @ aspectj ---
[WARNING] advice defined in org.javen.study.aspectj.c02.aspects.MockAuthenticationAspect has not been applied [Xlint:adviceDidNotMatch]
[WARNING] advice defined in org.javen.study.aspectj.c02.aspects.SecurityAspect has not been applied [Xlint:adviceDidNotMatch]
[WARNING] advice defined in org.javen.study.aspectj.c02.aspects.TrackingAspect has not been applied [Xlint:adviceDidNotMatch]

我还会附上以下所有相关的源代码: 负责主营业务的MessageCommunicator

package org.javen.study.aspectj.c02;

public class MessageCommunicator {
    public void deliver(String message) {
        System.out.println(message);
    }

    public void deliver(String person, String message) {
        System.out.println(person + ", " + message);
    }
}

将进行身份验证的身份验证器的简单版本:

public class Authenticator {

    public void authenticate() {
        System.out.println("authenticated");
    }
}

SecurityAspect会就MessageCommunicator提供建议:

package org.javen.study.aspectj.c02.aspects;

import org.javen.study.aspectj.c02.Authenticator;

public aspect SecurityAspect {
    private Authenticator authenticator = new Authenticator();

    declare warning 
        : call(void Authenticator.authenticate()) 
            && !within(SecurityAspect)
        : "Authentication should be performed only by SecurityAspect";

    pointcut secureAccess() : execution(* org.javen.study.aspectj.c02.MessageCommunicator.deliver(..));

    before() : secureAccess() {
        System.out.println("Checking and authenticating user");
        authenticator.authenticate();
    }
}
测试包中的

MockAuthenticationAspect建议验证者注入一些验证逻辑(不需要查看建议细节,建议实现有问题):

package org.javen.study.aspectj.c02.aspects;
import org.javen.study.aspectj.c02.Authenticator;

public aspect MockAuthenticationAspect {
    declare parents: Authenticator implements Callable;
    private boolean Callable.isCalled;

    public void Callable.call() {
        isCalled = true;
    }

    public boolean Callable.isCalled() {
        return isCalled;
    }

    Object around(Callable accessTracked)   : execution(* Authenticator.authenticate(..)) 
        && !execution(* Callable.*(..)) 
        && this(accessTracked) {
        accessTracked.call();
        return null;
    }

    private static interface Callable {
    }
}

整个项目的主题:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.javen.study</groupId>
    <artifactId>aspectj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <aspectj.version>1.7.3</aspectj.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <complianceLevel>1.6</complianceLevel>
                    <includes>
                        <include>**/*.java</include>
                        <include>**/*.aj</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>compile_with_aspectj</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile_with_aspectj</id>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

使用命令“mvn clean install”进行构建时,将打印警告文本,并且测试包建议将不起作用。但是,如果在eclipse中检查AJDT,所有的切入点和建议都有效。

有人可以帮助我吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

通过在测试编译执行​​中添加以下配置来解决问题。

<execution>
    <id>test-compile_with_aspectj</id>
    <goals>
        <goal>test-compile</goal>
    </goals>
    <configuration>
        <weaveDirectories>
            <weaveDirectory>target/classes</weaveDirectory>
        </weaveDirectories>
    </configuration>
</execution>

不知道这是一个好的实践,但至少现在它有效。