【问题标题】:Spring JMS junit test can't find @Component annotated receiverSpring JMS junit测试找不到@Component带注释的接收器
【发布时间】:2018-05-22 02:10:38
【问题描述】:

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

没有符合条件的 bean 类型 'br.com.framework.TestJMSReceiverImpl' 可用: 预计至少有 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 {

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? (它在正确的包装中吗?)
  • 它在 br.com.framework 包上,我猜它是正确的..

标签: java spring junit jms autowired


【解决方案1】:

TestJMSReceiverImpl 类未包含在您的测试上下文中。您需要为其添加SpringJmsApplicationTest 类的上下文配置:更改

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

进入

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2020-07-27
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多