【发布时间】:2017-11-10 00:27:16
【问题描述】:
问题是我无法在运行单元测试时模拟这个 aspectj 类,因为在我模拟它之前它以某种方式被注入到上下文中。
示例代码 -
@Aspect
public class ExampleAspect {
@Around ("execution * com.*.*.*(..)")
public void printResult(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before Method Execution");
joinPoint.proceed();
System.out.println("After Method Execution");
} }
测试类-
public class ClassATest
{
@Mock
private ExampleAspect mockExampleAspect;
private ClassA testClass;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
Mockito.doNothing().when(mockExampleAspect).printResult(anyList());
testClass = new ClassA();
}
@Test
public void test() {
// before and after methodA() is executed it is intercepted by the bean of ExampleAspect
testClass.methodA();
}
}
我能够成功使用这个方面。问题出在单元测试用例上。我如何模拟这个aspectj类或禁用单元测试用例的aspectj?谢谢
【问题讨论】:
标签: junit aspectj spring-aop