-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Junit5+Spring): support injecting MockHttpServletRequestBuilder …
…when using MockMvc tests #1334
- Loading branch information
Ronald Holshausen
committed
Mar 28, 2021
1 parent
bed5ba5
commit 3622c25
Showing
5 changed files
with
72 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...rc/main/kotlin/au/com/dius/pact/provider/spring/junit5/PactVerificationSpringExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package au.com.dius.pact.provider.spring.junit5 | ||
|
||
import au.com.dius.pact.core.model.Interaction | ||
import au.com.dius.pact.core.model.Pact | ||
import au.com.dius.pact.core.model.PactSource | ||
import au.com.dius.pact.provider.junit5.PactVerificationContext | ||
import au.com.dius.pact.provider.junit5.PactVerificationExtension | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.api.extension.ParameterContext | ||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder | ||
|
||
class PactVerificationSpringExtension( | ||
pact: Pact<Interaction>, | ||
pactSource: PactSource, | ||
interaction: Interaction, | ||
serviceName: String, | ||
consumerName: String? | ||
) : PactVerificationExtension(pact, pactSource, interaction, serviceName, consumerName) { | ||
constructor(context: PactVerificationExtension) : this(context.pact, context.pactSource, context.interaction, | ||
context.serviceName, context.consumerName) | ||
|
||
override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Boolean { | ||
val store = extensionContext.getStore(ExtensionContext.Namespace.create("pact-jvm")) | ||
val testContext = store.get("interactionContext") as PactVerificationContext | ||
return when (parameterContext.parameter.type) { | ||
MockHttpServletRequestBuilder::class.java -> testContext.target is MockMvcTestTarget | ||
else -> super.supportsParameter(parameterContext, extensionContext) | ||
} | ||
} | ||
|
||
override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any? { | ||
val store = extensionContext.getStore(ExtensionContext.Namespace.create("pact-jvm")) | ||
return when (parameterContext.parameter.type) { | ||
MockHttpServletRequestBuilder::class.java -> store.get("request") | ||
else -> super.resolveParameter(parameterContext, extensionContext) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters