cowinr
9/20/2019 - 6:48 AM

WireMock

@SpringBootTest
@RunWith(SpringRunner.class)
public class OkHttpExaxeFileUploaderTest {

    // Type under test
    private OkHttpExaxeFileUploader uploader;

    // Run WireMock on port 9999
    @Rule
    public WireMockRule wireMockRule = new WireMockRule(options().port(9999));

    @Test
    public void validFileUploadIsSuccess() throws Exception {

        // Mock Exaxe API to return 200 and a success response
        givenThat(
                post(
                        urlEqualTo("/apex/UploadFile")
                )
                        .willReturn(aResponse()
                                .withStatus(200)
                                .withHeader("Content-Type", "application/json")
                                .withBody(readStringFromFile("uploadFileResponse_success.json"))
                        )
        );

        String rates = readStringFromFile("exchange-rates.csv");

        boolean success = uploader.uploadToExaxe("ExchangeRate", "some.csv", rates);

        assertThat(success).isTrue();
    }
}
<dependency>
    <groupId>com.github.tomakehurst</groupId>
    <artifactId>wiremock-jre8</artifactId>
    <version>${wiremock-jre8.version}</version>
    <scope>test</scope>
</dependency>