Skip to main content

Simple and convenient way to fetch file content in Java8

If you've used Wiremock to mock an external service, you know its possible to set the response returned as a String.

When setting the mocked service response, I usually save the response from the real service in the src/test/resource folder and extract the file content using the method below.

private static String getResponse(String path) throws IOException {
  return new String(Files.readAllBytes(Paths.get(path)));
}

Here is how to call the method above fetching the content of a file located in the test resource folder.

getResponse("src/test/resources/responses/filename.json")

Comments

Popular posts from this blog

Mapping with ObjectMapper

Converting from one object structure to another is often time consuming, without adding real value. I recently came across the Modelmapper library, which reduces this task to a minimum. Add the dependency below to either your gradle og pom file. Gradle compile "org.modelmapper:modelmapper:2.3.0" Maven <dependency>   <groupId>org.modelmapper</groupId>   <artifactId>modelmapper</artifactId>   <version>2.3.0</version> </dependency> ModelMapper configuration  There is a wide range of configuration options, which can be found here ModelMapper Internal basket object public class Basket {   private UUID id;   private String productCode;   private String customerName;   private String orderContactEmail;   private String orderContactPhoneNo;   // Assume getters and setters } External Basket object public class Basket...