我应该如何测试具有方面的方法?
How should I test a method with an aspect around?
我正在尝试进行集成测试。这些是我的组件:
用户服务
@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()
切入点,但我得到:
if() pointcut designator isn't supported by Spring.
你能帮帮我吗?谢谢。
这是 spring 配置文件 的完美用例。您可以定义配置文件并将您的配置 classes 绑定到这些配置文件。绑定到配置文件的配置只有在配置文件处于活动状态时才会被激活。有关详细信息,请参阅我对相关问题的回答:。您将需要定义一个配置文件(例如:测试或集成测试)并在配置 class 中使用该配置文件为您的 AuthService
.
提供模拟实现
附带说明一下,我强烈建议您使用 AspectJ(最好是编译时编织)而不是 Spring AOP,因为它更强大。
我正在尝试进行集成测试。这些是我的组件:
用户服务
@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()
切入点,但我得到:
if() pointcut designator isn't supported by Spring.
你能帮帮我吗?谢谢。
这是 spring 配置文件 的完美用例。您可以定义配置文件并将您的配置 classes 绑定到这些配置文件。绑定到配置文件的配置只有在配置文件处于活动状态时才会被激活。有关详细信息,请参阅我对相关问题的回答:AuthService
.
附带说明一下,我强烈建议您使用 AspectJ(最好是编译时编织)而不是 Spring AOP,因为它更强大。