简体   繁体   English

测试Spring AOP方面

[英]Testing a Spring AOP Aspect

When writing aspects, how can I test that they do match and that they are invoked when I want them to? 在编写方面时,如何测试它们是否匹配以及在我需要时调用它们?

I'm using @Aspect declarations with Spring 2.5.6. 我正在使用Spring 2.5.6的@Aspect声明。


I don't care about the functionality, that's extracted and tested otherwise. 我不关心功能,否则提取和测试。

There's three different things to test here: 这里要测试三种不同的东西:

  1. Are your pointcuts matching what you expect? 你的切入点符合你的期望吗?
  2. Do your advice reference the right pointcut? 你的建议是否参考了正确的切入点?
  3. Does the advice do as you expect? 建议是否按预期进行?

To test the pointcuts, you can define some test types that have the same package/type/method signatures as the intended "real" targets, then define a test advice against the pointcuts to ensure they are matched (also define some types that shouldn't be matched to ensure the pointcuts aren't too liberal). 要测试切入点,您可以定义一些与预期“真实”目标具有相同包/类型/方法签名的测试类型,然后针对切入点定义测试建议以确保它们匹配(还定义了一些不应该的类型)匹配以确保切入点不太自由)。 I usually do this by defining the advice to do a callback to a method in the test target, setting a flag, then assert the flag was set. 我通常通过定义建议来对测试目标中的方法进行回调,设置标志,然后断言标志已设置。

To test the advice is trickier. 测试建议比较棘手。 I tend to delegate all the advice body to a normal method, then focus on testing the method rather than the advice. 我倾向于将所有建议体委托给普通方法,然后专注于测试方法而不是建议。

If you've done that, the only missing part is that your advice is applied to the correct pointcuts and actually calls the methods. 如果您已经这样做了,唯一缺少的部分是您的建议应用于正确的切入点并实际调用方法。 If you're concerned this might be a problem you can do this by creating another aspect that matches your advice execution and sets a flag to show the expected delegated method was invoked by the aspect, and override the method to do nothing. 如果您担心这可能是一个问题,您可以通过创建与您的建议执行匹配的另一个方面并设置一个标志来显示方面调用的预期委托方法,并覆盖该方法不执行任何操作。

I ended up creating something which is a bit of an integration test, in the following way: 我最终通过以下方式创建了一些集成测试的东西:

Created a Spring-aware JUnit test 创建了一个Spring-aware JUnit测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "aspects-test.xml" })
public class SomeAspectTest {

}

Created a spring configuration for this test which: 为此测试创建了一个弹簧配置:

  • enables @AspectJ usage; 启用@AspectJ用法;
  • configures my aspect with dummy dependencies 使用虚拟依赖项配置我的方面
  • declares a bean which should be picked up by the aspect 声明一个应该由方面拾取的bean

     <aop:aspectj-autoproxy /> <bean class="SomeAspect"> <property name="userDetailsSource"> <bean class="StubUserDetailsSource"/> </property> </bean> <bean class="DummyService"/> 

In the unit test I retrieve the dummy service and invoke its methods 在单元测试中,我检索虚拟服务并调用其方法

@Autowired
private DummyService _dummyService;

@Test(expected = ApplicationSecurityException.class)
public void adminOnlyFails() throws ApplicationSecurityException {

    _dummyService.adminOnly();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM