我应该如何测试具有方面的方法?

时间:2016-03-08 10:19:28

标签: spring integration-testing spring-aop

我正在尝试进行集成测试。这些是我的组成部分:

UsersService

@ManageSession
public List<T> get() {
    //...
    return something;
}

ManageSessionAspect

@Autowired
AuthService authService;

@Before("manageSessionMethod()") //methods annotated with @ManageSession
public void doSomething() {
    authService.doSomething();
}

我需要测试UsersService.get()方法。但我想禁用该方面,或者我希望能够模拟其中的AuthService

我尝试使用is()切入点,但我得到了:

  

如果()切入点指示符不受Spring支持。

你能帮帮我吗?感谢。

1 个答案:

答案 0 :(得分:1)

这是 spring profile 的完美用例。您可以定义配置文件并将配置类绑定到这些配置文件。只有在配置文件处于活动状态时,才会激活与配置文件关联的配置。有关详细信息,请参阅我对相关问题的回答:A: How to mock bean and avoiding NoUniqueBeanDefinitionException。您需要定义一个配置文件(例如:test或integration-test),并在配置类中使用该配置文件为您的AuthService提供模拟实现。

作为旁注,我强烈建议您使用AspectJ(最好是编译时编织)而不是Spring AOP,因为它更强大。