question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@PactBroker not reading Spring properties with JUnit 5

See original GitHub issue

If you provide system properties in Spring application.yml these won’t be read by the @PactBroker annotation when running with JUnit 5. This works fine if you provide them as normal system properties.

This issue seems to have been fixed for the JUnit 4 runners in here: https://github.com/DiUS/pact-jvm/issues/632

I can see that the problem has been fixed by adding a property resolver, and that it is also possible to pass a valueResolver attribute for the @PactBroker annotation. However, since you need a Spring context to be able to create such a resolver for Spring, it cannot be done with the annotation.

I also tried to add a resolver into PactVerificationContext in @BeforeEach but that didn’t seem to do the trick.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
uglyogcommented, Feb 22, 2020

I’ve created a new module pact-jvm-provider-junit5-spring with a TestTemplateInvocationContextProvider that extends the current JUnit 5 one (PactVerificationSpringProvider). This should allow you to configure all the properties in the Spring context.

1reaction
bijancncommented, Sep 20, 2021

Sure, sorry. In the documentation, it says

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Provider("Animal Profile Service")
@PactBroker
public class ContractVerificationTest {
  
    @TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
      context.verifyInteraction();
    }
    
}

So, the PactVerificationSpringProvider is used on the method but not on the class. This does not allow me to load e.g. pactbroker.host from the application.yaml. Instead, I need to do something like this

@ExtendWith(PactVerificationSpringProvider::class) // this allows loading from Spring Context and thus resources/application.yaml and env variables
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Provider("Animal Profile Service")
@PactBroker()
public class ContractVerificationTest {
Read more comments on GitHub >

github_iconTop Results From Across the Web

PactBroker not reading Spring properties with JUnit 5 #1023
If you provide system properties in Spring application.yml these won't be read by the @PactBroker annotation when running with JUnit 5.
Read more >
Provider test integration with pact broker for Spring Boot junit5 ...
However, @PactBroker annotation depends on the system properties. Is there a way to get this working for application properties via the Spring ......
Read more >
Pact junit runner | Pact Docs
Rule JUnit annotations, that will be run before/after each test of ... The pact broker loader was updated to allow system properties to...
Read more >
Consumer Tests with Pact, Junit5, SpringBoot - Level Up Coding
The following makes use of with Java, Spring and Junit5. ... You can use a pact broker to house and serve up pact...
Read more >
“How-to” Guides - Spring
Flow: Consumer Contract Approach with Pact Broker on the Producer Side; 7.9. ... the JUnit 4 rule, JUnit 5 extension, or properties, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found