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



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

没有合格的豆类型 'br.com.framework.TestJMSReceiverImpl' available: 预计至少有 1 个 Bean 符合自动连线候选条件。

测试类:

@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();
}
}

我的主要应用程序类:

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

The 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类不包含在测试上下文中。您需要将其添加到SpringJmsApplicationTest类的上下文配置中:更改

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

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

最新更新