Skip to content

Commit 74cf96b

Browse files
committedFeb 3, 2020
Use Before/AfterTestExecutionCallback
Use BeforeTestExecutionCallback and AfterTestExecutionCallback instead of BeforeEachCallback and AfterEachCallback, respectively.
1 parent 3860faa commit 74cf96b

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed
 

‎consumer/pact-jvm-consumer-junit5/src/main/kotlin/au/com/dius/pact/consumer/junit5/PactConsumerTestExt.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import au.com.dius.pact.core.support.expressions.ExpressionParser.parseExpressio
2121
import mu.KLogging
2222
import org.junit.jupiter.api.Disabled
2323
import org.junit.jupiter.api.extension.AfterAllCallback
24-
import org.junit.jupiter.api.extension.AfterEachCallback
24+
import org.junit.jupiter.api.extension.AfterTestExecutionCallback
2525
import org.junit.jupiter.api.extension.BeforeAllCallback
26-
import org.junit.jupiter.api.extension.BeforeEachCallback
26+
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback
2727
import org.junit.jupiter.api.extension.Extension
2828
import org.junit.jupiter.api.extension.ExtensionContext
2929
import org.junit.jupiter.api.extension.ParameterContext
@@ -133,7 +133,7 @@ class JUnit5MockServerSupport(private val baseMockServer: BaseMockServer) : Mock
133133
}
134134
}
135135

136-
class PactConsumerTestExt : Extension, BeforeEachCallback, BeforeAllCallback, ParameterResolver, AfterEachCallback, AfterAllCallback {
136+
class PactConsumerTestExt : Extension, BeforeTestExecutionCallback, BeforeAllCallback, ParameterResolver, AfterTestExecutionCallback, AfterAllCallback {
137137
override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Boolean {
138138
val store = extensionContext.getStore(ExtensionContext.Namespace.create("pact-jvm"))
139139
if (store["providerInfo"] != null) {
@@ -189,7 +189,7 @@ class PactConsumerTestExt : Extension, BeforeEachCallback, BeforeAllCallback, Pa
189189
store.put("executedFragments", mutableListOf<Method>())
190190
}
191191

192-
override fun beforeEach(context: ExtensionContext) {
192+
override fun beforeTestExecution(context: ExtensionContext) {
193193
val (providerInfo, pactMethod) = lookupProviderInfo(context)
194194

195195
logger.debug { "providerInfo = $providerInfo" }
@@ -295,7 +295,7 @@ class PactConsumerTestExt : Extension, BeforeEachCallback, BeforeAllCallback, Pa
295295
return pact
296296
}
297297

298-
override fun afterEach(context: ExtensionContext) {
298+
override fun afterTestExecution(context: ExtensionContext) {
299299
if (!context.executionException.isPresent) {
300300
val store = context.getStore(ExtensionContext.Namespace.create("pact-jvm"))
301301
val providerInfo = store["providerInfo"] as ProviderInfo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package au.com.dius.pact.consumer.junit5;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
7+
import au.com.dius.pact.consumer.MockServer;
8+
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
9+
import au.com.dius.pact.core.model.RequestResponsePact;
10+
import au.com.dius.pact.core.model.annotations.Pact;
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import org.apache.commons.collections4.MapUtils;
15+
import org.apache.commons.io.IOUtils;
16+
import org.apache.http.HttpResponse;
17+
import org.apache.http.client.fluent.Request;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
@ExtendWith(PactConsumerTestExt.class)
23+
@PactTestFor(providerName = "ArticlesProvider", port = "1234")
24+
public class BeforeEachTest {
25+
private String response;
26+
final private String EXPECTED_RESPONSE = "expected";
27+
28+
private Map<String, String> headers = MapUtils.putAll(new HashMap<>(), new String[] {
29+
"Content-Type", "text/plain"
30+
});
31+
32+
@BeforeEach
33+
void beforeEach() {
34+
response = EXPECTED_RESPONSE;
35+
}
36+
37+
@Pact(consumer = "Consumer")
38+
public RequestResponsePact pactExecutedAfterBeforeEach(PactDslWithProvider builder) {
39+
return builder
40+
.given("provider state")
41+
.uponReceiving("request")
42+
.path("/")
43+
.method("GET")
44+
.willRespondWith()
45+
.headers(headers)
46+
.status(200)
47+
.body(response)
48+
.toPact();
49+
}
50+
51+
@Test
52+
@PactTestFor(pactMethod = "pactExecutedAfterBeforeEach")
53+
void testPactExecutedAfterBeforeEach(MockServer mockServer) throws IOException {
54+
HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/").execute().returnResponse();
55+
assertThat(IOUtils.toString(httpResponse.getEntity().getContent()),
56+
is(equalTo(EXPECTED_RESPONSE)));
57+
}
58+
}

0 commit comments

Comments
 (0)
Please sign in to comment.