我正在尝试为Custom Aspect编写Integratation测试。这是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();
}
}
因此,只要关节点与切入点匹配,上述方面就会截获。它的工作正常。但我的问题是如何进行集成测试。
我所做的是在Aspect中创建属性“count”以进行跟踪并在我的Junit中声明它。我不确定这是好还是有更好的方法对方面进行集成测试。
这是Junit的片段,我做了什么。我提出的方式很糟糕,但我希望我对集成测试所做的工作不够。
@Test
public void testSamepleAspect(){
sampleAspect.intercept(mockJointPoint);
Assert.assertEquals(simpleAspect.getCount(),1);
}
答案 0 :(得分:1)
让我们使用与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();
}
}
您有多种选择,具体取决于您想要测试的内容:
随后我将描述选项号。 3.查看上面的示例代码,我们看到以下副作用:
方面的集成测试:
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
的模拟实例并确保为更大的正数引发预期的异常来测试我们的示例方面所具有的三种类型的副作用。< / p>