如何使用 Google Cloud Pub/Sub 进行 Junit 测试
Posted
技术标签:
【中文标题】如何使用 Google Cloud Pub/Sub 进行 Junit 测试【英文标题】:How to do Junit Test with Google Cloud Pub/Sub 【发布时间】:2015-10-22 17:40:28 【问题描述】:我在我的系统中使用 Google Cloud Pub/Sub 的 push pub/sub,我想构建我的 CI 测试代码,但我不知道该怎么做。例如,一些代码是这样的:
final Pubsub pubsub = PubsubUtils.getClient();
final PubsubMessage pubsubMessage = new PubsubMessage();
pubsubMessage.encodeData(message.getBytes(StandardCharsets.UTF_8));
Map<String, String> attrs = new HashMap<String, String>();
attrs.put("key", "value");
pubsubMessage.setAttributes(attrs);
final List<PubsubMessage> messages = ImmutableList.of(pubsubMessage);
final PublishRequest publishRequest = new PublishRequest().setMessages(messages);
final PublishResponse publishResponse = pubsub.projects().topics().publish(topic, publishRequest).execute();
final List<String> messageIds = publishResponse.getMessageIds();
还有这个:
final ServletInputStream reader = request.getInputStream();
try
// Parse the JSON message to the POJO model class.
final JsonParser parser = JacksonFactory.getDefaultInstance().createJsonParser(reader);
parser.skipToKey("message");
final PubsubMessage message = parser.parseAndClose(PubsubMessage.class);
Map<String, String> attrs = message.getAttributes();
String value = attrs.get("key");
// Base64-decode the data and work with it.
final String data = new String(message.decodeData(), StandardCharsets.UTF_8);
if (data != null || StringUtils.isNotEmpty(data))
Logger.getLogger("logger").info(data);
// Work with your message
// Respond with a 20X to acknowledge receipt of the message.
response.setStatus(HttpServletResponse.SC_OK);
finally
reader.close();
如何为这两部分代码编写一个正常的Junit测试用例?而且,我正在使用 PowerMockito 来模拟对象。
希望有人可以帮助我。
【问题讨论】:
【参考方案1】:对于第一个示例的单元测试,您可以使用 API 调用所需的返回值模拟 Pub/Sub 客户端对象,例如:
import com.google.api.services.pubsub.Pubsub;
import com.google.api.services.pubsub.Pubsub.Projects;
import com.google.api.services.pubsub.Pubsub.Projects.Topics;
import com.google.api.services.pubsub.Pubsub.Projects.Topics.Create;
import com.google.api.services.pubsub.Pubsub.Projects.Topics.Publish;
// ...
@Mock private Pubsub mockPubsub;
@Mock private Projects mockProjects;
@Mock private Topics mockTopics;
@Mock private Create mockCreate;
@Mock private Publish mockPublish;
// ...
String topicName = "projects/myproject/topics/mytopic";
String messageId = "messageId";
List<String> messageIds = ImmutableList.of(messageId);
PublishResponse publishResponse = new PublishResponse()
.setMessageIds(messageIds);
when(mockPubsub.projects()).thenReturn(mockProjects);
when(mockProjects.topics()).thenReturn(mockTopics);
when(mockTopics.publish(eq(topicName), isA(PublishRequest.class)))
.thenReturn(mockPublish);
when(mockPublish.execute()).thenReturn(publishResponse);
对于第二个示例,您可以模拟 HttpServletRequest
和 HttpServletResponse
,然后使用这些模拟对象调用 servlet。
【讨论】:
是的,第二个例子我已经做了,第一个例子还在进行中。 我认为第一个例子可以正常工作。谢谢!以上是关于如何使用 Google Cloud Pub/Sub 进行 Junit 测试的主要内容,如果未能解决你的问题,请参考以下文章
Google Cloud 上使用 Pub/Sub 的主/从模式
如何修改后台 Cloud Function 的 Google Cloud Pub/Sub 订阅确认截止日期
如何将 Google Cloud Platform Pub/Sub 消息推送到 C# 桌面应用程序