AspectJ的集成测试 [英] Integration tests for AspectJ

查看:146
本文介绍了AspectJ的集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Custom Aspect编写Integratation测试。这是Aspect Class Snippet。

I am trying to write Integratation tests for Custom Aspect. Here is the Aspect Class Snippet.

@Aspect
@Component
public class SampleAspect {

    private static Logger log = LoggerFactory.getLogger(SampleAspect.class);

   private int count;

   public int getCount(){
      return count;
   }

    public void setCount(){
      this.count= count;
    }


    @Around("execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))")
    public Object intercept(final ProceedingJoinPoint point) throws Throwable {
        logger.info("invoked Cutom aspect");
         setCount(1);
         return point.proceed();

    }

}

所以以上只要关节点与切入点匹配,方面就会截取。它的工作正常。但我的问题是如何执行集成测试。

So the above aspect intercepts whenever jointpoint matches the pointcut. Its working fine. But my question is how to perform Integration test.

我所做的是在Aspect中创建属性count以进行跟踪并在我的Junit中声明它。我不确定这是好还是有更好的方法对方面进行集成测试。

What I have done is I created the attribute "count" in Aspect for tracking and asserted it in my Junit. I am not sure if this is good or is there a better way of doing integration testing on aspects.

这是Junit的片段,我做了什么。我提出的方式很糟糕,但我希望我对集成测试所做的工作不尽如人意。

Here is the snippet of Junit what I have done. I presented in bad way but I hope its undestandable of what I have done for Integration testing.

@Test
public void testSamepleAspect(){
   sampleAspect.intercept(mockJointPoint);
   Assert.assertEquals(simpleAspect.getCount(),1);
}


推荐答案

让我们使用相同的样本代码,如我对相关AspectJ单元测试问题的回答

Let us use the same sample code as in my answer to the related AspectJ unit testing question:

方面要定位的Java类:

package de.scrum_master.app;

public class Application {
    public void doSomething(int number) {
        System.out.println("Doing something with number " + number);
    }
}

测试中的方面:

package de.scrum_master.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class SampleAspect {
    @Around("execution(* doSomething(int)) && args(number)")
    public Object intercept(final ProceedingJoinPoint thisJoinPoint, int number) throws Throwable {
        System.out.println(thisJoinPoint + " -> " + number);
        if (number < 0)
            return thisJoinPoint.proceed(new Object[] { -number });
        if (number > 99)
            throw new RuntimeException("oops");
        return thisJoinPoint.proceed();
    }
}

您有多种选择,具体取决于您想要的具体内容测试:

You have several options, depending on what exactly you want to test:


  1. 您可以运行AspectJ编译器并验证其控制台输出(启用编织信息)以确保预期连接点实际上是编织的而其他连接点则不是。但这更像是对AspectJ配置和构建过程的测试,而不是真正的集成测试。

  2. 同样,你可以创建一个新的编织类加载器,加载方面,然后一个几个类(加载时编织,LTW),以动态检查什么编织和什么不编织。在这种情况下,您更倾向于测试您的切入点是否比由核心+方面代码组成的集成应用程序更正。

  3. 最后,但并非最不重要的是,您可以执行正常的集成测试,假设如何应用程序应该在核心+方面代码正确编织后表现。如何做到这一点取决于你的具体情况,特别是你的方面添加到核心代码的副作用。

随后我将描述选项号。 3.查看上面的示例代码,我们看到以下副作用:

Subsequently I will describe option no. 3. Looking at the sample code above, we see the following side effects:


  • 对于小的正数,方面通过截取方法的原始 参数值,唯一的副作用是附加日志输出。

  • 对于负数,方面通过 否定 参数值(例如将-22转为22)到截获的方法,这是很好的可测试的。

  • 对于较大的正数,方面抛出 例外 ,有效地阻止了原始方法的执行。

  • For small positive numbers the aspect passes through the original parameter value to the intercepted method, the only side effect being additional log output.
  • For negative numbers the aspect passes through the negated parameter value (e.g. turning -22 into 22) to the intercepted method, which is nicely testable.
  • For larger positive numbers the aspect throws an exception, effectively stopping the original method from being executed at all.

方面的集成测试:

package de.scrum_master.aspect;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.PrintStream;

import org.junit.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import de.scrum_master.app.Application;

public class SampleAspectIT {
    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    private Application application = new Application();

    private PrintStream originalSystemOut;
    @Mock private PrintStream fakeSystemOut;

    @Before
    public void setUp() throws Exception {
        originalSystemOut = System.out;
        System.setOut(fakeSystemOut);
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(originalSystemOut);
    }

    @Test
    public void testPositiveSmallNumber() throws Throwable {
        application.doSomething(11);
        verify(System.out, times(1)).println(matches("execution.*doSomething.* 11"));
        verify(System.out, times(1)).println(matches("Doing something with number 11"));
    }

    @Test
    public void testNegativeNumber() throws Throwable {
        application.doSomething(-22);
        verify(System.out, times(1)).println(matches("execution.*doSomething.* -22"));
        verify(System.out, times(1)).println(matches("Doing something with number 22"));
    }

    @Test(expected = RuntimeException.class)
    public void testPositiveLargeNumber() throws Throwable {
        try {
            application.doSomething(333);
        }
        catch (Exception e) {
            verify(System.out, times(1)).println(matches("execution.*doSomething.* 333"));
            verify(System.out, times(0)).println(matches("Doing something with number"));
            assertEquals("oops", e.getMessage());
            throw e;
        }
    }
}

Etvoilà,我们正在测试通过检查日志输出到 System.out 的模拟实例并确保为更大的正数引发预期的异常,我们的示例方面具有三种类型的副作用。

Et voilà, we are testing exactly the three types of side effects our sample aspect has by inspecting log output to a mock instance of System.out and by making sure that the expected exception is thrown for larger positive numbers.

这篇关于AspectJ的集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆