My organization started using Pact to create / verify contracts between REST services / microservices written in Java about six months ago. We can hardly decide what the appropriate coverage or understanding of the supplier’s test should be, and he would like to receive some input from the experience of other users of the pact.
Basically, the discussion develops around where it is taunted / drowned out in provider tests. In a service, you will need to at least make fun of external calls to other services, but you have the opportunity to laugh at the REST resource class.
We welded it to two options:
1 .. The first option is that the provider test should be a strict test contract and use only the service provider’s REST service resource class, mock / call service classes / orchestrants, etc. used from there. This contract test will be supplemented by tests of components that will check parts that have been muffled / mocked by a vendor test.
2 .. The second option is to use the vendor test as a component test that will use the entire service component for each request. Only transient external calls to other components will be mocked / patched.
This is a pro thought for every option.
Pro for option 1:
- Tests will be easier to implement and will have a smaller size => higher degree of isolation.
- , , , ,
( ..). ,
(Pact .) , .
Pro 2:
, . ?
, "":
. " Martin Fowlers http://martinfowler.com/articles/microservice-testing/.
/ REST . Pact. :
@Path("/customer")
public class CustomerResource {
@Autowired private CustomerOrchestrator customerOrchestrator;
@GET
@Path("/{customerId}")
@Produces(MediaType.APPLICATION_JSON)
public Response get(@PathParam("customerId") String id) {
CustomerId customerId = CustomerIdValidator.validate(id);
return Response.ok(toJson(customerOrchestrator.getCustomer(customerId))).build();
}
@Autowired ( spring) CustomerOrchestrator , "Impl". "CustomerOrchestratorImpl.class", @Autowired bean, , , ... .. , DAO-, REST, HTTP- /.
" 1" , customerOrchestrator CustomerResource, " 2", Impl- ( ) CustomerResource .
, . , " 2", DAO-, , , .
" ", Autowired, spring, stubbing/mocking . , CustomerResource CustomerOrchestrator bean:
@RunWith(PactRunner.class)
@Provider("customer-rest-api")
@PactCachedLoader(CustomerProviderContractTest.class)
public class CustomerProviderContractTest {
@ClassRule
public static PactJerseyWebbAppDescriptorRule webAppRule = buildWebAppDescriptorRule();
@Rule
public PactJerseyTestRule jersyTestRule = new PactJerseyTestRule(webAppRule.appDescriptor);
@TestTarget public final Target target = new HttpTarget(jersyTestRule.port);
private static PactJerseyWebbAppDescriptorRule buildWebAppDescriptorRule() {
return PactJerseyWebbAppDescriptorRule.Builder.getBuilder()
.withContextConfigLocation("classpath:applicationContext-test.xml")
.withRestResourceClazzes(CustomerResource.class)
.withPackages("api.rest.customer")
.build();
}
@State("that customer with id 1111111 exists")
public void state1() throws Exception {
CustomerOrchestrator customerOrchestratorStub = SpringApplicationContext.getBean(CustomerOrchestrator.class)
when(customerOrchestratorStub.getCustomer(eq("1111111"))).thenReturn(createMockedCustomer("1111111));
}
...