pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 479 forks source link

[pact-jvm-provider-maven] beforeAll & afterAll #499

Open ndelacrz opened 7 years ago

ndelacrz commented 7 years ago

Hi, I've been checking the pact-jvm-provider-maven code, and could not see if there is an option to have some sort of a junit-style "@beforeAll" and "@afterAll", where it will execute those steps before and after ALL the pact-verify tests. Is there some way to set that kind of behavior? If not, is there any rationale why not (maybe it's not a good idea, or perhaps there is point against having it?)

uglyog commented 7 years ago

It's not that it's a bad idea, it is just that nobody has needed this functionality.

If you are using maven, there are other ways of doing this. From the maven documentation at http://maven.apache.org/surefire/maven-failsafe-plugin/:

The Maven lifecycle has four phases for running integration tests:

pre-integration-test for setting up the integration test environment. integration-test for running the integration tests. post-integration-test for tearing down the integration test environment. verify for checking the results of the integration tests.

So if the pact verification is running during the integration-test phase, you can have other plugins run in the pre- and post- phases.

Danny02 commented 6 years ago

Hi, sadly this is not possible at the moment. The build ends when the plugin notices pact test failures. So the post-integration-test phase of maven is never called. The pattern which is used by the failsafe plugin, is to have two goals. One goal which is run in the integration-test phase which only produces a test log, but will never fail. And a second goal which will run in the verify phase (comes after post-integration-test) which checks for errors in the log and fails the build.

I want to use the plugin with Spring Boot. I want to start the server in the pre-integration-test and stop it in the post-integration-test phase. The pact plugin fails the build to early so my server is not shut down.

Are you open to accept a pull request for this? What would be your ideas how to implement this?

uglyog commented 6 years ago

PRs are always welcome, it is what the open source world is built upon!

The best way would be to configuration the Mojo to be able to execute a before and after goal, even if the test fails. I would refer to how the Failsafe Plugin does this for integration tests.

Danny02 commented 6 years ago

@uglyog what is your stand on the three choices I lined out in my previous comment?

uglyog commented 6 years ago

I probably would change the existing goal to support a before and after goal.

uglyog commented 6 years ago

Actually, now that I think about it, two new goals would be better. One that runs the verification and records the results, and one that fails the build if the recorded results indicate a failure. I think that is how the integration plugin works.