You can generate your own mockdata at https://mockaroo.com/
Reading Mock Data file :
- Place your mock data in your resource folder
- Write the below piece of code to read your data
public class MockData { public static ImmutableList<Person> getPeople() throws IOException { InputStream inputStream = Resources.getResource("people.json").openStream(); String json = IOUtils.toString(inputStream); Type listType = new TypeToken<ArrayList<Person>>() { }.getType(); List<Person> people = new Gson().fromJson(json, listType); return ImmutableList.copyOf(people); } public static ImmutableList<Car> getCars() throws IOException { InputStream inputStream = Resources.getResource("cars.json").openStream(); String json = IOUtils.toString(inputStream); Type listType = new TypeToken<ArrayList<Car>>() { }.getType(); List<Car> cars = new Gson().fromJson(json, listType); return ImmutableList.copyOf(cars); }
build.gradle
plugins { id 'java' id 'application' } repositories { jcenter() } dependencies { implementation 'com.google.guava:guava:28.0-jre' testImplementation 'junit:junit:4.12' testImplementation 'com.google.guava:guava:11.0.2' testImplementation 'org.assertj:assertj-core:3.6.1' testImplementation 'com.google.code.gson:gson:2.3.1' testImplementation 'commons-io:commons-io:2.6' } application { mainClassName = 'java8.App' }
Below are the json file downloaded from mockaroo (people.json and cars.json)
Beans :
package beans; public class Car { private final Integer id; private final String make; private final String model; private final String color; private final Integer year; private final Double price; public Car(Integer id, String make, String model, String color, Integer year, Double price) { this.id = id; this.make = make; this.model = model; this.color = color; this.year = year; this.price = price; } public Integer getId() { return id; } public String getMake() { return make; } public String getModel() { return model; } public String getColor() { return color; } public Integer getYear() { return year; } public Double getPrice() { return price; } @Override public String toString() { return "Car{" + "id=" + id + ", make='" + make + '\'' + ", model='" + model + '\'' + ", color='" + color + '\'' + ", year=" + year + ", price=" + price + '}'; } }
package beans; public class Person { final Integer id; final String firstName; final String lastName; final String email; final String gender; final Integer age; public Person(Integer id, String firstName, String lastName, String email, String gender, Integer age) { this.id = id; this.firstName = firstName; this.lastName = lastName; this.email = email; this.gender = gender; this.age = age; } public Integer getId() { return id; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getEmail() { return email; } public String getGender() { return gender; } public Integer getAge() { return age; } @Override public String toString() { return "Person{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", gender='" + gender + '\'' + ", age=" + age + '}'; } }
package beans; public class PersonDTO { private final Integer id; private final String name; private final Integer age; public PersonDTO(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public String getName() { return name; } public Integer getAge() { return age; } @Override public String toString() { return "PeopleDTO{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } public static PersonDTO map(Person person) { return new PersonDTO(person.getId(), person.getFirstName(), person.getAge()); } }
Example :
@Test public void declarativeApproachUsingStreams() throws Exception { ImmutableList<Person> people = MockData.getPeople(); MockData.getPeople() .stream() .filter(p -> p.getAge() <= 18) .limit(10) .collect(Collectors.toList()) .forEach(System.out::println); }