Skip to content

Commit

Permalink
feat(JUnit): allow pact loader to setup from the test class instead o…
Browse files Browse the repository at this point in the history
…f just annotations
  • Loading branch information
uglyog committed Jun 29, 2022
1 parent 7b739a1 commit 3932b24
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/system-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Theses are all the system properties used by Pact-JVM
| pact.verification.reportDir | Verification (JUnit, JUnit 5) | Directory name | n | y | n | Sets the directory to write any configured verification reports to. |
| pactfolder.path | Verification (JUnit, JUnit 5) | Directory name | n | y | n | Directory to fetch pacts from when using the folder loader. |
| pact.verification.ignoreIoErrors | Verification (JUnit, JUnit 5) | true, false | n | y | n | When a test is annotated with @IgnoreNoPactsToVerify, any IO errors that occur while fetching the pacts will also be ignored . |
| pact.provider.name | Verification (JUnit 5) | string value | n | n | n | Sets the provider name to use when running the Pact verification tests. |
| pact.provider.name | Verification (JUnit 5) | string value | n | y | y | Sets the provider name to use when running the Pact verification tests. |
| pact.provider.version | Verification | string value | n | n | n | Sets the provider version to use when publishing verification results. |
| pact.provider.version.trimSnapshot | Verification | true, false | n | n | n | Enabling this will trim the Maven snapshot suffix off the Provider version. |
| pact.provider.tag | Verification | Provider names | y | y | n | List of provider tags to use to tag the verification results with when published back to the Pact Broker. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package au.com.dius.pact.provider.junit5

import au.com.dius.pact.core.model.Pact
import au.com.dius.pact.core.pactbroker.NotFoundHalResponse
import au.com.dius.pact.core.support.Utils
import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.expressions.ExpressionParser
import au.com.dius.pact.core.support.expressions.SystemPropertyResolver
Expand Down Expand Up @@ -56,7 +57,7 @@ open class PactVerificationInvocationContextProvider : TestTemplateInvocationCon
val serviceName = if (providerInfo.isPresent && providerInfo.get().value.isNotEmpty()) {
ep.parseExpression(providerInfo.get().value, DataType.STRING)?.toString()
} else {
System.getProperty("pact.provider.name")
Utils.lookupEnvironmentValue("pact.provider.name")
}
if (serviceName.isNullOrEmpty()) {
throw UnsupportedOperationException("Provider name should be specified by using either " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@
* Tags to use to fetch pacts for, defaults to `latest`
* If you set the tags through the `pactbroker.tags` system property, separate the tags by commas
*
* @deprecated Use {@link #consumerVersionSelectors} instead
* @deprecated Use consumerVersionSelectors method or pactbroker.consumerversionselectors property instead
*/
@Deprecated
String[] tags() default "${pactbroker.tags:}";

/**
* Consumer version selectors to fetch pacts for, defaults to latest version
* If you set the version selector tags or latest fields through system properties, separate values by commas
* @deprecated Use consumerVersionSelectors method or pactbroker.consumerversionselectors property instead
*/
@Deprecated
VersionSelector[] consumerVersionSelectors() default @VersionSelector(
tag = "${pactbroker.consumerversionselectors.tags:}",
latest = "${pactbroker.consumerversionselectors.latest:}",
Expand All @@ -69,7 +71,7 @@ VersionSelector[] consumerVersionSelectors() default @VersionSelector(
* Consumers to fetch pacts for, defaults to all consumers
* If you set the consumers through the `pactbroker.consumers` system property, separate the consumers by commas
*
* @deprecated Use {@link #consumerVersionSelectors} instead
* @deprecated Use consumerVersionSelectors method or pactbroker.consumerversionselectors property instead
*/
@Deprecated
String[] consumers() default "${pactbroker.consumers:}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface PactLoader {
PactSource getPactSource();

/**
* Sets the value resolver to use to resolve property expressions. By default a system property resolver will be used.
* Sets the value resolver to use to resolve property expressions. By default, a system property resolver will be used.
*
* @param valueResolver Value Resolver
*/
Expand All @@ -40,4 +40,9 @@ default void setValueResolver(ValueResolver valueResolver) { }
* Enables pending pact feature
*/
default void enablePendingPacts(boolean flag) { };

/**
* Supports additional initialisation using the test class
*/
default void initLoader(Class testClass) { };
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package au.com.dius.pact.provider

import au.com.dius.pact.core.model.DefaultPactReader
import au.com.dius.pact.core.model.FileSource
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.provider.junitsupport.loader.PactLoader
import au.com.dius.pact.provider.junitsupport.loader.PactSource
import mu.KLogging
Expand Down Expand Up @@ -134,13 +133,13 @@ object ProviderUtils : KLogging() {
return result
}

fun instantiatePactLoader(pactSource: PactSource, clazz: Class<*>, annotation: Annotation?): PactLoader {
fun instantiatePactLoader(pactSource: PactSource, testClass: Class<*>, annotation: Annotation?): PactLoader {
val pactLoaderClass = pactSource.value
return try {
val pactLoader = try {
// Checks if there is a constructor with one argument of type Class.
val constructorWithClass = pactLoaderClass.java.getDeclaredConstructor(Class::class.java)
constructorWithClass.isAccessible = true
constructorWithClass.newInstance(clazz)
constructorWithClass.newInstance(testClass)
} catch (e: NoSuchMethodException) {
logger.debug { "Pact source does not have a constructor with one argument of type Class" }
if (annotation != null) {
Expand Down Expand Up @@ -169,5 +168,7 @@ object ProviderUtils : KLogging() {
pactLoaderClass.createInstance()
}
}
pactLoader.initLoader(testClass)
return pactLoader
}
}

0 comments on commit 3932b24

Please sign in to comment.