单元测试由@Around建议建议的方法

时间:2015-07-09 22:01:00

标签: java spring unit-testing junit

我目前正在为使用Spring和AspectJ的应用程序中的类编写JUnit单元测试。被测试的类有一些公共方法,由方面类中的around-advice方法建议。 Aspect有一些注入的字段在执行建议的方法时变为null,即使我已经在测试应用程序上下文中成功实例化了这些bean,并且当调用它们的方法时,它们会抛出nullpointerexceptions。这是代码的简化版本:

要测试的课程:

public class ClassUnderTest {

    @Inject
    private Foo foo;

    @Audit(StringValue="arg", booleanValue=true)
    public Object advisedMethod() {
        Object ret = new Object();
        //things happen
        return ret;
    }

方面:

@Aspect
@Configurable
public class AuditAspect implements Versionable {

    @Inject
    Foo foo;

    @Inject
    Bar bar;

    @Around("@annotation(Audit)")
    public Object aroundAdvice(ProceedingJoinPoint pjp, Audit audit) {

        // Things happen
        privMethod(arg);
        // Yet other things happen
        Object ret = pjp.proceed();
        // Still more things happen
        return ret;
    }

    private Object privMethod(Object arg) {
        // Things continue to happen.
        // Then this throws a NullPointerException because bar is null.
        bar.publicBarMethod(arg2);
        // Method continues ...
    }
}

审核界面:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Audit {
    String value() default "";
    boolean bool() default false;
}

提供应用程序上下文的Config文件:

import static org.mockito.Mockito.*;

@Configuration
public class ClassUnderTestTestConfig {

    @Bean
    Foo foo() {
        return mock(Foo.class);
    }

    @Bean
    Bar bar() {
        return mock(Bar.class);
    }

    @Bean
    ClassUnderTest classUnderTest() {
        return new ClassUnderTest();
    }

    @Bean
    @DependsOn({"foo", "bar"})
    Aspect aspect() {
        return new Aspect();
    }
}

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
    @ContextConfiguration(classes = ClassUnderTestTestConfiguration.class),
    @ContextConfiguration(classes = ClassUnderTest.class)
})
public class ClassUnderTestTest {

    private static final String sessionNumber = "123456";

    @Inject
    Foo foo;

    @Inject
    Bar bar;

    @Inject
    ClassUnderTest classUnderTest;

    @Inject
    Aspect aspect;

    @Test
    public void test() {
        // This call triggers the advice which ends up throwning
        // a NullPointerException.
        classUnderTest.advised();
    }
}

我也尝试制作自己的Spring代理,然后按照this stack overflow post中的建议手动添加方面,方法是将此代码添加到测试类中:

@Before
public void setUp() {
    DataPointRestWebService target = new DataPointRestWebService();
    AspectJProxyFactory proxyMaker = new AspectJProxyFactory(target);
    proxyMaker.addAspect(auditAspect);
    dataPointRestWebService = proxyMaker.getProxy();
}

但最终会抛出:

AopConfigException: Advice must be declared inside an aspect type. Offending method 'public java.lang.Object Aspect.aroundAdvice' in class [Aspect]

我发现这个很神秘,因为我在Aspect类之前确实有@Aspect注释,并且该类在测试环境之外工作。

我对Spring和AspectJ很新,所以我完全接受这样的观点,即我认为这一切都是错的。我在这里提供了虚拟代码,希望省略无用的细节,但也因为工作代码是专有的而不是我的。如果您认为我遗漏了一个重要的细节,请告诉我,我会尝试添加它。

提前感谢您的帮助,如果我遗漏任何重要信息,请告诉我。

编辑:

根据请求,我添加了完整的NullPointerException堆栈跟踪:

java.lang.NullPointerException
    at com.unifiedcontrol.aspect.AuditAspect.getCaller(AuditAspect.java:265)
    at com.unifiedcontrol.aspect.AuditAspect.ajc$inlineAccessMethod$com_unifiedcontrol_aspect_AuditAspect$com_unifiedcontrol_aspect_AuditAspect$getCaller(AuditAspect.java:1)
    at com.unifiedcontrol.aspect.AuditAspect.aroundAuditAdvice(AuditAspect.java:79)
    at com.unifiedcontrol.server.rest.DataPointRestWebServiceTest.dummyTest(DataPointRestWebServiceTest.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

AuditAspect == Aspect和DataPointRestWebService == ClassUnderTest。

1 个答案:

答案 0 :(得分:1)

以下是解决问题的方法:

我们在ClassUnderTestTestConfig类中更改了以下方法:

@Bean
@DependsOn({"foo", "bar"})
Aspect aspect() {
    return new Aspect();
}

为:

@Bean
@DependsOn({"foo", "bar"})
Aspect aspect() {
    return Aspects.aspectOf(Aspect.class);
}

我们为其添加了以下import语句:

import org.aspectj.lang.Aspects;

原始代码成功返回了一个新的Aspect对象,但是当调用ClassUnderTest.advisedMethod()时,联合点被委托给另一个没有注入非null foo和bar成员的Aspect对象。有关Aspects.aspectOf()方法如何工作的事情确保了TestConfig创建的Aspect对象是为对adviceMethod()的调用提供建议的对象。

此刻我不知道为什么这解决了这个问题。工作中的其他人找到了解决方案。我打算调查一下并编辑这篇文章的更多信息,但同时欢迎所有的贡献。