There are many ways can be used to verify Provider, maven, junit, spring…..
Only list 2 ways below to check the verify process.
Build Plug-in
We use maven plug-in to verify Provider, you can refer the details in previous article Pact: Simple Usage.
The biggest disadvantage using this way is that we need to have provider running and pre-condition set up, e.g., prepare testing data.
Testing Framework
For JVM based provider, we can use pact junit provider runner to verify it. we can use junit framework, and don’t need to run provider, verify the exact provider code instead of all, use the JUnit setup and tear-down as well as a state annotation, and can also using mocking framework.
Add mockito & pact-jvm-provider-spring to maven pom
1
2
3
4
5
6
7
8
9
10
11
12
13<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/au.com.dius/pact-jvm-provider-spring -->
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-spring_2.12</artifactId>
<version>3.5.24</version>
</dependency>Consumer test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64public class ConsumerTest {
@Rule
public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
@Pact(provider="test_provider", consumer="test_consumer")
public RequestResponsePact getPersonAge(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
DslPart resbody = new PactDslJsonBody()
.stringType("name", "zhangsan")
.integerType("age", 4)
.asBody();
return builder
.uponReceiving("A request for age for person zhangsan")
.path("/person/zhangsan")
.method("GET")
.willRespondWith()
.status(200)
.headers(headers)
.body(resbody)
.toPact();
}
@Pact(provider="test_provider", consumer="test_consumer")
public RequestResponsePact getUser(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
DslPart resbody = new PactDslJsonBody()
.stringType("id", "1")
.stringType("name", "tanghulu")
.integerType("age", 4)
.asBody();
return builder
.given("user 1 exist")
.uponReceiving("A request go get user 1")
.path("/user")
.method("GET")
.willRespondWith()
.status(200)
.headers(headers)
.body(resbody)
.toPact();
}
@Test
@PactVerification(value="test_provider", fragment="getPersonAge")
public void doTest(){
Integer age = new GetPersonAge(mockProvider.getUrl()).checkAge("zhangsan");
System.out.println("Test result, age = " + age);
assertTrue(age <= 6);
}
@Test
@PactVerification(fragment="getUser")
public void getUser(){
String user = new GetUser(mockProvider.getUrl()).getUser();
System.out.println("Test result, user: " + user);
assertTrue(user.contains("tanghulu" ));
}Verify provider - state and mock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32@RunWith(RestPactRunner.class) // Say JUnit to run tests with custom Runner
@Provider("test_provider") // Set up name of tested provider
@PactFolder("../pact-consumer/target/pacts") // Point where to find pacts (See also section Pacts source in documentation)
@VerificationReports({"console", "markdown"})
public class VerifyState {
@Mock
private ProviderService providerService;
@InjectMocks
private ProviderController providerController = new ProviderController();
@Before //Method will be run before each test of interaction
public void before() {
//initialize your mocks using your mocking framework
MockitoAnnotations.initMocks(this);
//configure the MockMvcTarget with your controller and controller advice
target.setControllers(providerController);
}
@State("user 1 exist")
public void addUser(){
when(providerService.getUser())
.thenReturn(new ProviderModel("1", "tanghulu", 5));
}
@TestTarget // Annotation denotes Target that will be used for tests
public final MockMvcTarget target = new MockMvcTarget(); // Out-of-the-box implementation of Target (for more information take a look at Test Target section)
}