Hello, I have hard time with PactBroker annotation...
# pact-jvm
m
Hello, I have hard time with PactBroker annotation. When trying to run tests I get:
Copy code
Invalid pact broker host specified ('${pactbroker.host:}'). Please provide a valid host or specify the system property 'pactbroker.host'.
java.lang.IllegalArgumentException: Invalid pact broker host specified ('${pactbroker.host:}'). Please provide a valid host or specify the system property 'pactbroker.host'.
My application.yaml has:
Copy code
pactbroker:
  host: "<https://valid-host>"
  port: 443
  protocol: "https"
  auth:
    token: "tokenValue"
I use gradle and my only pact dep is:
Copy code
testImplementation("au.com.dius.pact.provider:junit5spring:4.2.11")
My Test:
Copy code
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Provider("provider")
@PactBroker
public class PactTest extends AbstractIntegrationTest {

  @LocalServerPort int port;

  @Value("${pactbroker.host}")
  String host;

  @BeforeEach
  public void setupTestTarget(PactVerificationContext context) {
    System.out.println("host");
    System.out.println(host);
    context.setTarget(new HttpTestTarget("localhost", port));
  }

  @TestTemplate
  @ExtendWith(PactVerificationSpringProvider.class)
  public void pactVerificationTestTemplate(PactVerificationContext context) {
    context.verifyInteraction();
  }
}
Host value is assigned correctly in test file. Why Pact doesn't see these values? What should I change?
j
I don't think that the Pact JUnit runner does anything with the application.yml, or anything Spring-specific for that matter. You can set these values via System environment variables. Or you could also look at the ValueResolver API, and then configure the @PactBroker annotation to use your custom ValueResolver
m
You don't seem to be using the PactBroker annotation above, and are just assigning it to an unused String. Is that intended?
m
@JapuDCret This example tells something different: https://docs.pact.io/implementation_guides/jvm/provider/junit5spring/ @Matt (pactflow.io / pact-js / pact-go) yes it was intended - just printing to make sure the properties are correctly grabbed from application.yaml Please take a look at this example: https://github.com/pact-foundation/pact-workshop-Maven-Springboot-JUnit5/tree/step11#step-11---using-a-pact-broker
👍 1
m
Have you checked the dependencies? I don't k one, but it seems it's a spring/pact integration that would be needed there
m
The same spring boot version: 2.4.3
m
Ah sorry, I don't really speak java. I'm sure it's going to be something simple tho
Hopefully somebody else can spot it!
m
Anybody can help?
j
@Marcin Baranowski I think the problem is how you're reading the spring properties. You'll want to use the
@PactBroker
annotation. e.g.
Copy code
@PactBroker(
        url = "${pactbroker.url}",
        consumerVersionSelectors = {
            @VersionSelector(tag = "master"),
            @VersionSelector(tag = "prod"),
            @VersionSelector(tag = "dr")
        })
🙏 1