用弹簧测试测试注释处理器

时间:2018-01-23 03:41:58

标签: java junit annotations spring-aop spring-test

我正在使用junit来测试我的注释处理器。测试失败了。似乎永远不会进入连接点,没有任何例外。 我的注释是这样的:

@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface EventListener {
    boolean isListening() default true;
    boolean isDefaultListener() default true;
    Class<SomeListener> subscriber() default None.class;
    public static class None implements SomeListener {
    ... ...
    }
}

处理器是这​​样的:

@Aspect
public class ListenerProcessor {
    @Pointcut("@annotation(com.xxx.common.event.annotation.EventListener)")
    public void sign() {}

    @Before("sign()")
    public void anAdvice(JoinPoint joinPoint) {   ***//this has never executed***
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        EventListener anno = method.getAnnotation(EventListener.class);
        if (anno != null) {
            if (anno.isListening()) {
                if (anno.isDefaultListener())
                    doDefault();
                else { 
                    Class<SomeListener> clazz = anno.subscriber();
                    doCustomize(clazz);
                }
            } else {
                ... ...
            }
        }
    }
... ...
}

我的测试是这样的:

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class EventListenerTest {
    @Test
    public final void test() {
        //given
        Long bef = countEvents();
        //when
        TestEntity1 t1 = appWithDefaultListener();
        //then
        TestEntity1 t2 = getEntityLike(t1);
        Long aft = countEvents();
        assertThat(t1).isEqualToComparingFieldByField(t2);
        assertThat(aft).isEqualTo(bef+1);
    }

    @Transactional
    @EventListener(isListening=true, isDefaultListener=true) ***//this seems does'nt work***
    private TestEntity1 appWithDefaultListener() {
        TestEntity1 t1 = new TestEntity1(...);
        return myRepository.save(t1);
    }

    @Transactional(readOnly = true)
    private TestEntity1 getEntityLike(TestEntity1 t1) {
        TestEntity1 t2 = myRepository.findOne(Example.of(t1));
        return t2;
    }
}

我的applicationContext.xml是这样的:

<aop:aspectj-autoproxy proxy-target-class="true">
    <aop:include name="eventProcessor"/>
</aop:aspectj-autoproxy>
<context:annotation-config />
<bean id="eventProcessor"
    class="com.xxx.common.event.process.EventListenerProcessor" 
    scope="prototype"/>

我的pom有这些依赖关系:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.13.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.13</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.13.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>2.6.0</version>
    </dependency>

我错过了什么?或者出了什么问题?

1 个答案:

答案 0 :(得分:0)

不要在测试类中将appWithDefaultListener()getEntityLike()定义为private方法。

相反,您需要将它们定义为您在测试ApplicationContext中配置为bean的另一个类中的非私有方法。然后将该bean @Autowired放入您的测试类中,并通过该引用调用方法,以便应用您的方面。