简体   繁体   中英

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

I'm trying to test JMSSender and JMSReceiver, the JMSSender is autowiring correctly, but the JMSReceiver is not.

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

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

main Application class i have:

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

The JMSReceiver:

public abstract class JMSReceiver {

    @Autowired
    JmsTemplate jmsTemplate;

    private String queue;

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

Anyone knows what I am missing here?

The TestJMSReceiverImpl class is not included in your test context. You need to add it the context configuration for the SpringJmsApplicationTest class: change

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

into

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM