PubSub 发布消息的 Junit 测试用例

问题描述 投票:0回答:1

在为以下代码编写 Junit 测试用例时遇到问题。

public String publishJSON(String json) throws InterruptedException, IOException, ExecutionException {             
        log.info(" Publishing payload to: "+config.getTopicId());   
        TopicName topicName=TopicName.of(config.getPubsubProjectId(),config.getTopicId());
        Publisher publisher=null;
        try {
             publisher =
                    Publisher.newBuilder(topicName)
                        .build();
              ByteString data = ByteString.copyFromUtf8(json);
              PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
              ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
              String messageId = messageIdFuture.get();
              log.info("Published message ID: " + messageId);
           return messageId;
        } catch (ExecutionException e) {
            log.error("Error while publishing messsage" + e.getMessage());
            throw e;
        } catch (IOException e) {
            log.error( "PubSub exception "+ e.getMessage());
            throw e;
        } catch (InterruptedException e) {
            log.error("Connection making exception for PubSub" + e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error( "publishJSON Error : "+ e.getMessage());
            throw e;    
        }
        finally {
            if (publisher != null) {
                // When finished with the publisher, shutdown to free up resources.
                publisher.shutdown();
                publisher.awaitTermination(1, TimeUnit.MINUTES);
            }
        }
    }
java junit4 google-cloud-pubsub
1个回答
0
投票

不是答案,只是试图解释问题。 您不能模拟 Publisher.Builder 类 因此,发生的任何操作

© www.soinside.com 2019 - 2024. All rights reserved.