【问题标题】:AspectJ advice not being executed through unit testAspectJ 建议未通过单元测试执行
【发布时间】:2013-06-06 20:43:10
【问题描述】:

我被难住了。我正在尝试测试 AspectJ 类。当我运行我的应用程序时,我的 Aspect 类被完美地拾起。但是,我似乎无法让任何 Aspect 类拦截测试中的任何方法。

我正在使用 Spring 3.2.2、AspectJ 1.7.2 和 Maven 4。

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

Test 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 failed to load 错误。

我错过了什么?

【问题讨论】:

    标签: java spring maven aspectj


    【解决方案1】:

    您将单元测试也用作@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})

    【讨论】:

    • 我按照你的建议移动了一些东西。它看起来确实更干净。但它仍然不起作用。现在,我在代码的 src/main/java 区域中拥有除测试类之外的所有内容,而测试类本身在 src/test/java 中。 TestAOP 在 WebConfig 中注解。可能与运行测试时发生编织的时间和方式有关吗?我需要在某个地方设置配置才能让它发生吗?我对 Spring 很陌生。
    • 哦,天哪,刚刚注意到您在测试中手动创建目标 bean TestAOPClient client = new TestAOPClient();... 它不能以这种方式工作。 Spring 必须是创建 bean 的那个人!我会更新我的答案。
    【解决方案2】:

    您的切入点配置错误(包应该是 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(..))")
    

    应该可以。

    【讨论】:

    • 这是我为了不暴露公司名称而更改包名时的错字。我查看了这些软件包,它们是正确的..
    • 感谢您的回答。我遇到了与@Emilie 类似的问题,而我的@Pointcut 表达式是阻止我的@Before 建议运行的最后一个问题!
    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多