如何在Spring Boot中为JMS客户端编写JUnit测试用例

问题描述

我必须在ActiveMQ Artemis中为生产者和消费者编写单元测试。我正在使用发布-订阅模型。如何为两者编写测试用例?

以下是我的生产者代码:

@Component
public class OrderPublisher {
    @Autowired
    JmsTemplate jmsTemplate;
    private static final Logger log=LoggerFactory.getLogger(OrderPublisher.class) ;

    @Value("${jsa.activemq.topic}")
    String orderTopic;

    public void sendOrderData(String orderData) {
        log.info("sending message to queue");
        jmsTemplate.convertAndSend(orderTopic,orderData);   
    }
}

这是我对生产者的应用程序属性:

spring.jms.pub-sub-domain=true
jsa.activemq.topic=bt-order-queue

spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=admin
spring.artemis.password=admin

这是我的消费者代码,位于一个单独的项目中。

@Component
public class OrderSubscriber {
 
    private final RestTemplate restTemplate = new RestTemplate();

    ObjectMapper objectMapper= new ObjectMapper();
    private static final Logger log = LoggerFactory.getLogger(OrderSubscriber.class);

    @JmsListener(destination = "${jsa.activemq.topic}")
    public void receiveMessage(String order) throws JsonMappingException,JsonProcessingException {
        System.out.println("order data from queue::"+order);
    }
}

供消费者使用的属性文件:

spring.artemis.mode=native
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=admin
spring.artemis.password=admin
spring.jms.pub-sub-domain=true
jsa.activemq.topic=bt-order-queue

这是我要生产的产品:

@RunWith(SpringRunner.class)
@SpringBootTest

class ApplicationTests {    
    
    @Autowired
    OrderPublisher publisher;   
    
    @Rule
    public EmbeddedActiveMQResource resource = new EmbeddedActiveMQResource();
     
    //ActiveMQProducerResource producer = new ActiveMQProducerResource(resource.getVmURL(),"bt-order-queue");
    //ActiveMQConsumerResource consumer = new ActiveMQConsumerResource(resource.getVmURL(),"bt-order-queue");
     
    @Before
    public void before() {
        resource.start();
        resource.createQueue("bt-order-queue");
    }
    
    @Test
    void contextLoads() {       
        publisher.sendOrderData("test");
        
        ClientMessage cons=resource.receiveMessage("bt-order-queue",100000);
        System.out.println("cons::"+cons.toString());       
    }
}

但是由于我缺少有关如何编写的信息,所以我只是想方设法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...