Is there any way to test that an error has been th...
# pact-jvm
a
Is there any way to test that an error has been thrown during the pact test
Copy code
@Provider("provider")
@PactFolder("pacts")
@ExtendWith(VertxExtension.class)
public class PactContractTest {

    final static AuthSessionRepository authSessionRepositoryMock = mock(AuthSessionRepository.class);
    final static AuthCodeRepository authCodeRepositoryMock = mock(AuthCodeRepository.class);
    final static DateUtils dateUtilsMock = mock(DateUtils.class);
    final static WebClient webClientMock = mock(WebClient.class, RETURNS_DEEP_STUBS);

    static WebClient webTestClient;

    @BeforeAll
    static void init(Vertx vertx, VertxTestContext testContext) {
        var options = new WebClientOptions().setDefaultPort(8888);
        webTestClient = WebClient.create(vertx, options);
        TestSetupHelper.setupAndDeployVerticleForTests(vertx, testContext, authSessionRepositoryMock, authCodeRepositoryMock, dateUtilsMock, webClientMock);
    }

    @BeforeEach
    void set_context(PactVerificationContext context) {
        context.setTarget(new HttpTestTarget("localhost", 8888, "/"));
    }

    @TestTemplate
    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactVerificationTestTemplate(VertxTestContext testContext, PactVerificationContext context) throws MalformedURLException {
        context.verifyInteraction();
        testContext.completeNow();
    }

    @State("returns id token as jwt")
    void create_id_token_as_jwt(){

    }

    @State("token error")
    void assert_correct_error_is_thrown_for_bad_token_request(PactVerificationContext context) {
      // Assert that a certain exception is thrown
    }



}
in one of my tests I am expecting a custom error to be thrown, can I catch it in my pact test or is that out of the scope of pact?
t
I’m not sure what you’re asking for - this looks like a pact verification, so wouldn’t the error be marshalled and sent as a response?
If yes, you can just put the error payload in the pact test like normal
a
There's an error sent in the response, that is tested in the pact interaction. There is also an internal error that is thrown in java, I was just asking if I can somehow assert that, the specific error has been thrown
With state, it runs before the pact test, so I can't expect an error to have been thrown when the test hasn't run yet
That's why I was asking if this was outside the scope of pact
u
I assume the error that is thrown in Java is on the consumer side? On the provider side, if the response is an error, but matches what is in the Pact file, the test will pass.