AspectJ 的集成测试

kar*_*hik 6 java junit integration-testing aspectj mockito

我正在尝试为自定义方面编写集成测试。这是方面类代码段。

@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();

    }

}
Run Code Online (Sandbox Code Playgroud)

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

我所做的是在 Aspect 中创建了用于跟踪的属性“count”,并在我的 Junit 中对其进行了断言。我不确定这是否很好,或者是否有更好的方法对方面进行集成测试。

这是我所做的 Junit 的片段。我以糟糕的方式呈现,但我希望它对我为集成测试所做的工作是不可理解的。

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

kri*_*aex 4

让我们使用与我对相关 AspectJ 单元测试问题的回答相同的示例代码:

\n

按方面定位的 Java 类:

\n
package de.scrum_master.app;\n\npublic class Application {\n    public void doSomething(int number) {\n        System.out.println("Doing something with number " + number);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

测试方面:

\n
package de.scrum_master.aspect;\n\nimport org.aspectj.lang.ProceedingJoinPoint;\nimport org.aspectj.lang.annotation.Around;\nimport org.aspectj.lang.annotation.Aspect;\n\n@Aspect\npublic class SampleAspect {\n    @Around("execution(* doSomething(int)) && args(number)")\n    public Object intercept(final ProceedingJoinPoint thisJoinPoint, int number) throws Throwable {\n        System.out.println(thisJoinPoint + " -> " + number);\n        if (number < 0)\n            return thisJoinPoint.proceed(new Object[] { -number });\n        if (number > 99)\n            throw new RuntimeException("oops");\n        return thisJoinPoint.proceed();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
    \n
  1. 您可以运行 AspectJ 编译器并验证其控制台输出(启用编织信息),以确保预期的连接点实际上已编织,而其他连接点则未编织。但这宁愿是对 AspectJ 配置和构建过程的测试,而不是真正的集成测试。
  2. \n
  3. 类似地,您可以创建一个新的编织类加载器,加载方面,然后加载一些类(加载时编织,LTW),以便动态检查哪些被编织,哪些没有。在这种情况下,您正在测试切入点是否正确,而不是测试由核心+方面代码组成的集成应用程序。
  4. \n
  5. 最后但并非最不重要的一点是,您可以执行正常的集成测试,假设正确编织核心 + 方面代码后应用程序应如何运行。如何做到这一点,取决于您的具体情况,特别是您的方面向核心代码添加了什么样的副作用。
  6. \n
\n

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

\n
    \n
  • 对于较小的正数,方面将原始参数值传递给拦截的方法,唯一的副作用是额外的日志输出。
  • \n
  • 对于负数,aspect将取反的参数值(例如将-22变成22)传递给被拦截的方法,这是很好的可测试性。
  • \n
  • 对于较大的正数,方面会抛出异常,从而有效地阻止原始方法的执行。
  • \n
\n

方面的集成测试:

\n
package de.scrum_master.aspect;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.mockito.ArgumentMatchers.matches;\nimport static org.mockito.Mockito.times;\nimport static org.mockito.Mockito.verify;\n\nimport java.io.PrintStream;\n\nimport org.junit.*;\nimport org.junit.Before;\nimport org.junit.Rule;\nimport org.junit.Test;\nimport org.mockito.Mock;\nimport org.mockito.junit.MockitoJUnit;\nimport org.mockito.junit.MockitoRule;\n\nimport de.scrum_master.app.Application;\n\npublic class SampleAspectIT {\n    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();\n\n    private Application application = new Application();\n\n    private PrintStream originalSystemOut;\n    @Mock private PrintStream fakeSystemOut;\n\n    @Before\n    public void setUp() throws Exception {\n        originalSystemOut = System.out;\n        System.setOut(fakeSystemOut);\n    }\n\n    @After\n    public void tearDown() throws Exception {\n        System.setOut(originalSystemOut);\n    }\n\n    @Test\n    public void testPositiveSmallNumber() throws Throwable {\n        application.doSomething(11);\n        verify(System.out, times(1)).println(matches("execution.*doSomething.* 11"));\n        verify(System.out, times(1)).println(matches("Doing something with number 11"));\n    }\n\n    @Test\n    public void testNegativeNumber() throws Throwable {\n        application.doSomething(-22);\n        verify(System.out, times(1)).println(matches("execution.*doSomething.* -22"));\n        verify(System.out, times(1)).println(matches("Doing something with number 22"));\n    }\n\n    @Test(expected = RuntimeException.class)\n    public void testPositiveLargeNumber() throws Throwable {\n        try {\n            application.doSomething(333);\n        }\n        catch (Exception e) {\n            verify(System.out, times(1)).println(matches("execution.*doSomething.* 333"));\n            verify(System.out, times(0)).println(matches("Doing something with number"));\n            assertEquals("oops", e.getMessage());\n            throw e;\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

System.outet voil\xc3\xa0,我们通过检查模拟实例的日志输出并确保对于较大的正数抛出预期的异常,来准确测试示例方面的三种类型的副作用。

\n