pact-jvm icon indicating copy to clipboard operation
pact-jvm copied to clipboard

@State with parameters and @PactVerifyProvider

Open holomekc opened this issue 10 months ago • 1 comments

Hi,

I am currently facing the situation that with an async message interaction we need to "prepare" a state with parameters before verifying. So e.g.:

@QuarkusTest
class MyProviderTest {

  @InjectLocalStack
  LocalStack localStack;

  @Inject
  ExternalServiceInterface esi;

  @State
  void prepareState(final Map<String, Object> params){
     var parameters = params.get("parameters");
  }

  @PactVerifyProvider("something important")
  String provideMessage() {
    var data = new SomeData().setValue("test").setAnotherValue(123);  

    // TODO: How to access parameters? :(
    esi.publishMessage(data, parameters);

    var publishedMessage = localStack.getSqsMessage();
    // do some assertions
    return publishedMessge;
  }
}

There is not much logic behind the esi using the provided parameters of the state, but there is a little bit. So I am not sure if it is really worth it. Nevertheless, maybe you have a best practice for that?

I could find some implementations where @State and @PactVerifyProvider was used together. But they annotated the same method. Due to the fact that we use QuarkusTests, and at least at the moment, we needed to add a lot of workarounds to make this even work (Classloader issue), we needed to reimplement some of the underlaying logic. Therefore, we could see how the library works with the annotated methods. Using the same method seems to be a bad idea. The lib would execute the same method twice. Once for the state preparation and then a second time for the verification. Calling the esi twice is wrong in my opinion and also could result in unexpected behavior.

But I have no other idea. I have two methods now and would need to pass the content to the provideMessage method. Sure I could store some information in the test itself, but this is a bit strange as well.

Without the parameters it is fine, but with parameters I am a bit lost. Maybe we use it wrong. Any suggestions?

Br, Chris

holomekc avatar Aug 28 '23 21:08 holomekc