spring-jms - junit spring jms 监听器

标签 spring-jms spring-junit

我想对下面的一个简单的 jms 监听器代码进行单元测试

@Component
public class NotificationReader {

    @JmsListener(destination = "myAppQ")
    public void receiveMessage(NotificationMessage notificationMessage) {
        System.out.println("Received <" + notificationMessage.getStaffNumber() + ">");
    }

}

在 junit 中,我使用 jmsTemplate 将消息泵入 Active MQ。

我想测试是否调用了 jms 监听器。

我看到很少有像 How to wait for @JMSListener annotated method to complete in JUnit 这样的解决方案(使用计数器) ,这实际上只是为了测试目的而更改了监听器代码,而我不想这样做。

还有其他选择吗?


尝试按照答案中的建议进行配置。

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestNotificationReader {

    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${outbound.endpoint}")
    private String destination;

    @Test
    public void contextLoads() {
    }

    @Test
    public void testPutToQ() {
        NotificationMessage notificationMessage = new NotificationMessage();
        notificationMessage.setStaffNumber("100");
        notificationMessage.setWorkflowType("TYPE");
        notificationMessage.setWorkflowId("100");
        notificationMessage.setWorkflowDescription("Test From Queue");
        jmsTemplate.convertAndSend(destination, notificationMessage);

        jmsTemplate.setReceiveTimeout(10000);

        try {
            TestConfig.latch.await(10, TimeUnit.SECONDS);

            NotificationMessage temp = (NotificationMessage) TestConfig.received;

            System.out.println(" temp.getStaffNumber() " + temp.getStaffNumber());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Configuration
    public static class TestConfig {

        private static final CountDownLatch latch = new CountDownLatch(1);

        private static Object received;

        @Bean
        public static BeanPostProcessor listenerWrapper() {
            return new BeanPostProcessor() {

                @Override
                public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                    if (bean instanceof NotificationReader) {
                        MethodInterceptor interceptor = new MethodInterceptor() {

                            @Override
                            public Object invoke(MethodInvocation invocation) throws Throwable {
                                Object result = invocation.proceed();
                                if (invocation.getMethod().getName().equals("receiveMessage")) {
                                    received = invocation.getArguments()[0];
                                    latch.countDown();
                                }
                                return result;
                            }

                        };
                        if (AopUtils.isAopProxy(bean)) {
                            ((Advised) bean).addAdvice(interceptor);
                            return bean;
                        } else {
                            ProxyFactory proxyFactory = new ProxyFactory(bean);
                            proxyFactory.addAdvice(interceptor);
                            return proxyFactory.getProxy();
                        }
                    } else {
                        return bean;
                    }
                }

                @Override
                public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                    // TODO Auto-generated method stub
                    return bean;
                }

            };
        }

    }

}

添加 testConfig 后,JMSTempate Autowiring 失败

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

最佳答案

将您的监听器 bean 包装在代理中(用于测试用例)并使用锁存器并验证接收到的对象是否符合您的预期...

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { So48033124Application.class, So48033124ApplicationTests.TestConfig.class })
public class So48033124ApplicationTests {

    @Autowired
    private JmsTemplate template;

    @Test
    public void test() throws Exception {
        Foo foo = new Foo("bar");
        this.template.convertAndSend("foo", foo);
        assertThat(TestConfig.latch.await(10, TimeUnit.SECONDS)).isTrue();
        assertThat(TestConfig.received).isEqualTo(foo);
    }

    @Configuration
    public static class TestConfig {

        private static final CountDownLatch latch = new CountDownLatch(1);

        private static Object received;

        @Bean
        public static BeanPostProcessor listenerWrapper() {
            return new BeanPostProcessor() {

                @Override
                public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                    if (bean instanceof MyListener) {
                        MethodInterceptor interceptor = new MethodInterceptor() {

                            @Override
                            public Object invoke(MethodInvocation invocation) throws Throwable {
                                Object result = invocation.proceed();
                                if (invocation.getMethod().getName().equals("listen")) {
                                    received = invocation.getArguments()[0];
                                    latch.countDown();
                                }
                                return result;
                            }

                        };
                        if (AopUtils.isAopProxy(bean)) {
                            ((Advised) bean).addAdvice(interceptor);
                            return bean;
                        }
                        else {
                            ProxyFactory proxyFactory = new ProxyFactory(bean);
                            proxyFactory.addAdvice(interceptor);
                            return proxyFactory.getProxy();
                        }
                    }
                    else {
                        return bean;
                    }
                }

            };
        }

    }

}

编辑

以上内容基于 Spring Framework 5 或更高版本,它使用 Java 8 并为 BeanPostProcessor 方法提供默认实现。

如果您使用的是早期版本的 Spring,您还需要

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    return bean;
}

BPP 也应该是static

关于spring-jms - junit spring jms 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48033124/

相关文章:

java - @JmsListener 的 JUnit 测试

spring-boot - 来自 .properties 文件的 Spring Boot 单元测试 @Value 给出 NullPointerException

spring - @ExtendWith(SpringExtension.class) 不工作

java - 多个事务管理器 NoUniqueBeanDefinitionException

java - 如何在 Spring JMS 连接工厂中配置 weblogic 重新连接策略

java - 无法运行 Junit 案例。抛出错误 "Actually there were zero interactions with this mock"

java - 运行 Spring 集成测试时为 "org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same"

java - 由于 "received expired message",无法使用来自 ActiveMQ 的消息

java - Apache CXF-JMS 3.0 和 Spring 配置