我如何从JUnit测试Spring Boot中排除JMS配置?
@Configuration
@EnableJms
public class MqConfiguration {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${ibm.mq.host}")
private String host;
@Value("${ibm.mq.port}")
private int port;
@Value("${ibm.mq.channel}")
private String channel;
@Value("${ibm.mq.queueManager}")
private String queueManager;
@Value("${ibm.mq.queueName}")
private String queueName;
@Value("${trade-publisher.env}")
private String env;
@Bean
public MQQueue mqQueue() throws MQException {
logger.info("MqConfiguration.mqQueue() : Creating Queue Manager.");
if (!"test".equalsIgnoreCase(env)) {
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(MQConstants.HOST_NAME_PROPERTY, host);
properties.put(MQConstants.PORT_PROPERTY, port);
properties.put(MQConstants.CHANNEL_PROPERTY, channel);
MQQueueManager mqQueueManager = new MQQueueManager(this.queueManager, properties);
int openOptions = MQConstants.MQOO_OUTPUT;
return mqQueueManager.accessQueue(this.queueName, openOptions);
}
return null;
}
}
我尝试在测试中添加@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {MqConfiguration.class})})
,但仍然尝试连接到MQ。
@ActiveProfiles(profiles = {"test"})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"camel.springboot.java-routes-include-pattern=**/NONE*"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, SecurityAutoConfiguration.class})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {MqConfiguration.class})})
public class MainApplicationTest {
@MockBean
private ReferenceDataCache cacheMock;
@MockBean
private MqService mqService;
@Test
public void test() {
MainApplication.main(new String[]{});
}
}