Spring JMS junit 测试找不到@Component 注释的接收器

Spring JMS junit test can't find @Component annotated receiver

我正在尝试测试 JMSSender 和 JMSReceiver,JMSSender 自动装配正确,但 JMSReceiver 不是。

No qualifying bean of type 'br.com.framework.TestJMSReceiverImpl' available: expected at least 1 bean which qualifies as autowire candidate.

测试class:

@RunWith(SpringRunner.class)
@DirtiesContext
@ContextConfiguration(classes = { JMSSenderConfig.class, JMSReceiverConfig.class })
@TestPropertySource(properties = { "spring.activemq.broker-url = vm://localhost:61616" })
public class SpringJmsApplicationTest {

    @ClassRule
    public static EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();

    @Autowired
    private JMSSender sender;

    @Autowired
    private TestJMSReceiverImpl receiver;

    @Test
    public void testReceive() throws Exception {
        sender.send("helloworld.q", "Daleee");
        receiver.receive();
    }
}

主要应用程序 class 我有:

@Configuration
@ComponentScan("br.com.framework")
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class, DataSourceAutoConfiguration.class })
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

TestJMSReceiverImpl:

@Component
public class TestJMSReceiverImpl extends JMSReceiver {

    public TestJMSReceiverImpl() {
        super("helloworld.q");
    }
    ...
}

JMSReceiver:

public abstract class JMSReceiver {

    @Autowired
    JmsTemplate jmsTemplate;

    private String queue;

    public JMSReceiver(String queue) {
        this.queue = queue;
    }
    ...
}

有人知道我在这里遗漏了什么吗?

TestJMSReceiverImpl class 未包含在您的测试上下文中。您需要将其添加到 SpringJmsApplicationTest class 的上下文配置中:change

@ContextConfiguration(classes = { JMSSenderConfig.class, JMSReceiverConfig.class })

进入

@ContextConfiguration(classes = { JMSSenderConfig.class,
JMSReceiverConfig.class, TestJMSReceiverImpl.class })