• <i id='ytdps'><tr id='ytdps'><dt id='ytdps'><q id='ytdps'><span id='ytdps'><b id='ytdps'><form id='ytdps'><ins id='ytdps'></ins><ul id='ytdps'></ul><sub id='ytdps'></sub></form><legend id='ytdps'></legend><bdo id='ytdps'><pre id='ytdps'><center id='ytdps'></center></pre></bdo></b><th id='ytdps'></th></span></q></dt></tr></i><div id='ytdps'><tfoot id='ytdps'></tfoot><dl id='ytdps'><fieldset id='ytdps'></fieldset></dl></div>

        <tfoot id='ytdps'></tfoot>
          <bdo id='ytdps'></bdo><ul id='ytdps'></ul>

        <small id='ytdps'></small><noframes id='ytdps'>

      1. <legend id='ytdps'><style id='ytdps'><dir id='ytdps'><q id='ytdps'></q></dir></style></legend>

        AspectJ 的集成测试

        时间:2023-09-25

        <small id='oxCkM'></small><noframes id='oxCkM'>

          <legend id='oxCkM'><style id='oxCkM'><dir id='oxCkM'><q id='oxCkM'></q></dir></style></legend>

              1. <tfoot id='oxCkM'></tfoot>

                • <bdo id='oxCkM'></bdo><ul id='oxCkM'></ul>
                    <tbody id='oxCkM'></tbody>
                  <i id='oxCkM'><tr id='oxCkM'><dt id='oxCkM'><q id='oxCkM'><span id='oxCkM'><b id='oxCkM'><form id='oxCkM'><ins id='oxCkM'></ins><ul id='oxCkM'></ul><sub id='oxCkM'></sub></form><legend id='oxCkM'></legend><bdo id='oxCkM'><pre id='oxCkM'><center id='oxCkM'></center></pre></bdo></b><th id='oxCkM'></th></span></q></dt></tr></i><div id='oxCkM'><tfoot id='oxCkM'></tfoot><dl id='oxCkM'><fieldset id='oxCkM'></fieldset></dl></div>
                  本文介绍了AspectJ 的集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  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 单元测试问题:

                  要按方面定位的 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:

                  • 对于小的正数,aspect 通过 original 参数值传递给被拦截的方法,唯一的副作用是额外的日志输出.
                  • 对于负数,aspect 会通过 negated 参数值(例如将 -22 变为 22)传递到被截获的方法,这是很好的可测试性.
                  • 对于较大的正数,方面会引发异常,从而完全停止执行原始方法.

                  方面的集成测试:

                  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;
                          }
                      }
                  }
                  

                  等等,我们正在通过检查日志输出到 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 的集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito spy - 当调用内部类方法而不是间谍对象中的 下一篇:为什么没有“从不分配字段"?@Mock 警告

                  相关文章

                  最新文章

                  <tfoot id='yWOs1'></tfoot>
                    1. <small id='yWOs1'></small><noframes id='yWOs1'>

                      <i id='yWOs1'><tr id='yWOs1'><dt id='yWOs1'><q id='yWOs1'><span id='yWOs1'><b id='yWOs1'><form id='yWOs1'><ins id='yWOs1'></ins><ul id='yWOs1'></ul><sub id='yWOs1'></sub></form><legend id='yWOs1'></legend><bdo id='yWOs1'><pre id='yWOs1'><center id='yWOs1'></center></pre></bdo></b><th id='yWOs1'></th></span></q></dt></tr></i><div id='yWOs1'><tfoot id='yWOs1'></tfoot><dl id='yWOs1'><fieldset id='yWOs1'></fieldset></dl></div>
                      • <bdo id='yWOs1'></bdo><ul id='yWOs1'></ul>

                    2. <legend id='yWOs1'><style id='yWOs1'><dir id='yWOs1'><q id='yWOs1'></q></dir></style></legend>