我被难住了。我正在尝试测试 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 似乎被拾取了,因为在运行时,如果我为我的方面切入点指定了一个不存在的类,我将收到 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})
。
关于java - AspectJ 建议未通过单元测试执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16971819/