https://pact.io logo
#pact-jvm
Title
# pact-jvm
w

Wiesław Bondyra

05/27/2022, 11:05 AM
Hello, I am trying to test POST request with multipart but I have got error likr this java: cannot access org.json.JSONObject class file for org.json.JSONObject not found I am using au.com.dius.pact.consumerjunit54.2.21 version. My Pact and test is here:
Copy code
@Pact(consumer = CONSUMER_NAME, provider = PROVIDER_NAME)
    public RequestResponsePact createPactForWiesaTest(PactDslWithProvider pactDslWithProvider) {

        MultipartEntityBuilder multipart2 = MultipartEntityBuilder.create()
                .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                .setContentType(ContentType.APPLICATION_FORM_URLENCODED)
                .addBinaryBody("file", "1,2,3".getBytes(), ContentType.create("text/csv"), "data.csv")
                .addTextBody("textPart", "sample text");

        PactDslResponse builder = pactDslWithProvider
                .given("Check buildId")
                .uponReceiving("Request to get wiesa buildId")
                .path("/wiesabuild")
                .body(multipart2)
                .method("POST")
                .willRespondWith()
                .status(HttpStatus.OK.getCode())
                .body("is OK", MediaType.TEXT_PLAIN);
        return builder.toPact();
    }

    @Test
    @PactTestFor(providerName = "WiesaMultipartService", pactMethod = "createPactForWiesaTest")
    void testWiesaMultipartService(MockServer mockServer) throws MalformedURLException {
        String url = mockServer.getUrl();
        String response;

        MultipartEntityBuilder multipart2 = MultipartEntityBuilder.create()
                .setMode(HttpMultipartMode.STRICT)
                .setContentType(ContentType.APPLICATION_FORM_URLENCODED)
                .addBinaryBody("file", "1,2,3".getBytes(), ContentType.create("text/csv"), "data.csv")
                .addTextBody("textPart", "sample text");

        try (RxHttpClient rxHttpClient = RxHttpClient.create(new URL(url))) {
            response = rxHttpClient.toBlocking().retrieve(
                    <http://HttpRequest.POST|HttpRequest.POST>("/wiesabuild", multipart2)
                            .contentType(APPLICATION_FORM_URLENCODED)
                            .accept(APPLICATION_FORM_URLENCODED),
                    String.class
            );
            assertThat(response).as("body should be with simple is OK statement").isEqualToIgnoringWhitespace("is OK");
        }
    }
What might be wrong in this example?
Seems that adding _testImplementation_("org.jsonjson20190722") in my project fixes issue. Why it is required? Should not it be in pact.consumer dependency?
2 Views