Skip to content

Commit

Permalink
feat: add example JUnit4 Spring test using new consumer version selec…
Browse files Browse the repository at this point in the history
…tor method
  • Loading branch information
uglyog authored and rholshausen committed Nov 24, 2022
1 parent a601f18 commit 041e69b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package au.com.dius.pact.provider.spring;

import au.com.dius.pact.provider.junit.target.HttpTarget;
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
import au.com.dius.pact.provider.junitsupport.loader.PactBrokerConsumerVersionSelectors;
import au.com.dius.pact.provider.junitsupport.loader.SelectorBuilder;
import au.com.dius.pact.provider.junitsupport.target.Target;
import au.com.dius.pact.provider.junitsupport.target.TestTarget;
import au.com.dius.pact.provider.spring.testspringbootapp.TestApplication;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@RunWith(SpringRestPactRunner.class)
@Provider("myAwesomeService")
@PactBroker(url = "http://broker.host")
@IgnoreNoPactsToVerify(ignoreIoErrors = "true")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { TestApplication.class })
public class ConsumerVersionSelectorTest {
@TestTarget
public final Target target = new HttpTarget(8332);

static boolean called = false;
@PactBrokerConsumerVersionSelectors
public static SelectorBuilder consumerVersionSelectors() {
called = true;
return new SelectorBuilder().branch("current");
}

@AfterClass
public static void after() {
assertThat("consumerVersionSelectors() was not called", called, is(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package au.com.dius.pact.provider.spring.testspringbootapp;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class TestApplication {

public static class ObjectThatMustBeClosed {
private ObjectThatMustBeClosed() {}

private static final ObjectThatMustBeClosed instance = new ObjectThatMustBeClosed();
public static ObjectThatMustBeClosed getInstance() {
return instance;
}

public boolean destroyed = false;

public void shutdown() {
destroyed = true;
}
}

@Bean(destroyMethod="shutdown")
ObjectThatMustBeClosed mustBeClosed() {
return ObjectThatMustBeClosed.getInstance();
}
}

0 comments on commit 041e69b

Please sign in to comment.