|
| 1 | +package au.com.dius.pact.provider.junit5; |
| 2 | + |
| 3 | +import au.com.dius.pact.provider.junitsupport.Provider; |
| 4 | +import au.com.dius.pact.provider.junitsupport.State; |
| 5 | +import au.com.dius.pact.provider.junitsupport.loader.PactFolder; |
| 6 | +import org.junit.jupiter.api.extension.Extension; |
| 7 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 8 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 9 | +import org.junit.jupiter.api.extension.ParameterResolutionException; |
| 10 | +import org.junit.jupiter.api.extension.ParameterResolver; |
| 11 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | +import org.junit.jupiter.api.TestTemplate; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | + |
| 17 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 18 | +import static org.hamcrest.Matchers.is; |
| 19 | +import static org.hamcrest.Matchers.notNullValue; |
| 20 | + |
| 21 | +@Provider("myAwesomeService") |
| 22 | +@PactFolder("pacts") |
| 23 | +public class WithRegisteredExtensionTest { |
| 24 | + private static final Logger LOGGER = LoggerFactory.getLogger(WithRegisteredExtensionTest.class); |
| 25 | + |
| 26 | + @RegisterExtension |
| 27 | + static final TestDependencyResolver resolverExt = new TestDependencyResolver(/*...*/); |
| 28 | + |
| 29 | + private final TestDependency dependency; |
| 30 | + |
| 31 | + public WithRegisteredExtensionTest(TestDependency dependency) { |
| 32 | + this.dependency = dependency; |
| 33 | + } |
| 34 | + |
| 35 | + @TestTemplate |
| 36 | + @ExtendWith(PactVerificationInvocationContextProvider.class) |
| 37 | + void test() { |
| 38 | + assertThat(dependency, is(notNullValue())); |
| 39 | + } |
| 40 | + |
| 41 | + @State("state 2") |
| 42 | + void state2() { |
| 43 | + } |
| 44 | + |
| 45 | + @State("default") |
| 46 | + void stateDefault() { |
| 47 | + } |
| 48 | + |
| 49 | + static class TestDependencyResolver implements Extension, ParameterResolver { |
| 50 | + @Override |
| 51 | + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { |
| 52 | + return parameterContext.getParameter().getType().isAssignableFrom(TestDependency.class); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { |
| 57 | + return new TestDependency(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static class TestDependency { |
| 62 | + } |
| 63 | +} |
0 commit comments