如何解决在Java EE的Pact使用者测试中使用服务
我想在我们的Java EE应用程序中实施Pact使用者测试。该测试应调用将触发实际REST调用的消费者服务方法。
到目前为止,这是公约测试:
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "my-service")
public class MyServiceConsumerTest {
@Inject
private MyService myService;
@Pact(consumer = "myConsumer")
public RequestResponsePact mail(PactDslWithProvider builder) {
Map<String,String> headers = new HashMap<>();
headers.put("Content-Type",ContentType.getJSON().asstring());
PactDslJsonBody jsonBody = new PactDslJsonBody()
.stringValue("emailAddress","foo@bar.com")
.stringValue("subject","Test subject")
.stringValue("content","Test content")
.asBody();
return builder
.given("DEFAULT_STATE")
.uponReceiving("request for sending a mail")
.path("/mail")
.method("POST")
.headers(headers)
.body(jsonBody)
.willRespondWith()
.status(Response.Status.OK.getStatusCode())
.toPact();
}
@Test
@PactTestFor(pactMethod = "mail")
public void sendMail() {
MailNotification mailNotification = MailNotification.builder()
.emailAddress("foo@bar.com")
.subject("Test subject")
.content("Test content")
.build();
myService.sendNotification(mailNotification);
}
}
有趣的部分是这一行:
myService.sendNotification(mailNotification);
在运行消费者单元测试时,无法注入MyService
,即导致myService
为null
。此外,我认为有必要告诉服务针对Pact模拟serveR发送请求吗?
当然我可以在测试中触发最终的REST请求,但这会忽略服务逻辑。
我想我在这里缺少什么?
解决方法
是的,您应该在@PactVerification
测试中访问模拟服务器。不要没有实际的应用程序代码而解雇,这在将来进行更改时会很有意义。如果您更改了该请求的HTTP属性,则测试应该会失败
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。