没有通过单元测试执行AspectJ通知



我被难住了。我正在尝试测试一个AspectJ类。当我运行我的应用程序时,我的Aspect类得到了完美的拾取。然而,我似乎无法让任何Aspect类拦截测试中的任何方法。

我使用Spring 3.2.2, AspectJ 1.7.2和Maven 4。

下面是我正在使用的简单测试:

测试AspectJ类
package my.package.path.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class TestAOP {
    private String message;
    public TestAOP() {
    }
    @Pointcut("execution(* my.package.path.TestAOPClient.relayMessage(..))")
    public void aopPointcut() {
    }
    @Around("aopPointcut()")
    public String monitor(ProceedingJoinPoint pjp) throws Throwable {
        String msg = (String)pjp.proceed();
        this.setMessage(msg);
        return msg;
    }

}

方法被拦截的类

package my.package.path.config;
public class TestAOPClient {
    public String relayMessage(String msg) {
        return msg;
    }
}

测试类

package my.package.path.config;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@Configuration
@ContextConfiguration(classes={WebConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/main/java")
public class AopConfigTest extends AbstractJUnit4SpringContextTests {
     @Bean
     public TestAOP testAop() throws Exception {
        return new TestAOP();
     }
     @Test
     public void assertTestConfigIsActive() {
        TestAOPClient client = new TestAOPClient();
        client.relayMessage("hello");
        assertThat(((TestAOP)applicationContext.getBean("testAop")).getMessage(), equalTo("hello"));
    }
}

WebConfig文件

package my.package.path.web.context;
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass=false)
@ComponentScan(value={"my.package.path.config", "my.package.path.web"})
public class WebConfig {
}

总是得到断言错误

 Expected: "hello" but: was null

我的WebApplicationContext似乎被拾取了,因为在运行时,如果我为我的Aspect切入点指定一个不存在的类,我会得到一个ApplicationContext未能加载错误。

我错过了什么?

您使用单元测试也作为@Configuration源,这有点奇怪。

您应该从单元测试中删除@Configuration注释,并将testAOP() bean定义移动到WebConfig。但最重要的是,被建议的bean 不能手动创建,而是由Spring:

创建。
@ContextConfiguration(classes={WebConfig.class})
@WebAppConfiguration("src/main/java")
public class AopConfigTest extends AbstractJUnit4SpringContextTests {
     @Autowired
     private TestAOP testAop;
     @Autowired
     private TestAOPClient client;
     @Test
     public void assertTestConfigIsActive() {
        client.relayMessage("hello");
        assertThat(((TestAOP)applicationContext.getBean("testAop")).getMessage(), 
            equalTo("hello"));
    }
}

使用bean定义更新配置:

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass=false)
@ComponentScan(value={"my.package.path.config", "my.package.path.web"})
public class WebConfig {
    @Bean
    public TestAOP testAop() throws Exception {
        return new TestAOP();
    }
    @Bean
    public TestAOPClient testAopClient() throws Exception {
        return new TestAOPClient();
    }
}

如果您的目标是测试AOP配置是否有效,并且TestAOP确实是一个测试bean(而不仅仅是这个问题的假名称),那么您可以创建一个特殊的TestConfig配置类,将bean定义移到那里,并从测试@ContextConfiguration(classes={WebConfig.class,TestConfig.class})中使用它。

您的切入点配置是错误的(包应该是my.package.path.config以匹配您的测试客户端)

@Pointcut("execution(* my.package.path.TestAOPClient.relayMessage(..))")

和你的客户端是

package my.package.path.config;
public class TestAOPClient {
...

改成

@Pointcut("execution(* my.package.path.config.TestAOPClient.relayMessage(..))")

最新更新