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)
让我们使用与我对相关 AspectJ 单元测试问题的回答相同的示例代码:
\n按方面定位的 Java 类:
\npackage 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}\nRun Code Online (Sandbox Code Playgroud)\n测试方面:
\npackage 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}\nRun Code Online (Sandbox Code Playgroud)\n您有多种选择,具体取决于您想要测试的内容:
\n随后我将描述选项号。3.查看上面的示例代码,我们看到以下副作用:
\n方面的集成测试:
\npackage 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}\nRun Code Online (Sandbox Code Playgroud)\nSystem.outet voil\xc3\xa0,我们通过检查模拟实例的日志输出并确保对于较大的正数抛出预期的异常,来准确测试示例方面的三种类型的副作用。