@Test public void shouldFindCarsByExample() throws Exception { String uniqueColor = "unique-color-" + UUID.randomUUID().toString(); Car exampleCar = CarBuilder.aCar() .withManufacturer(UUID.randomUUID().toString()) .withColor(uniqueColor) .build(); saveNewCopiesOfCarToRepository(exampleCar, 10); Car secondExampleCar = CarBuilder.aCar() .withManufacturer(UUID.randomUUID().toString()) .withColor(uniqueColor) .build(); saveNewCopiesOfCarToRepository(secondExampleCar, 5); Car searchByManufacturerExampleCar = new Car(null, 0, exampleCar.getManufacturer(), null); List<Car> foundCars = carRepository.findByExample(searchByManufacturerExampleCar); assertThat(foundCars).isNotNull().hasSize(10); Car searchByColorExampleCar = new Car(null, 0, null, uniqueColor); List<Car> foundCarsByColor = carRepository.findByExample(searchByColorExampleCar); assertThat(foundCarsByColor).isNotNull().hasSize(15); Car searchByManufacturerAndColorExampleCar = new Car(null, 0, exampleCar.getManufacturer(), uniqueColor); List<Car> foundCarsByManufacturerAndColor = carRepository.findByExample(searchByManufacturerAndColorExampleCar); assertThat(foundCarsByManufacturerAndColor).isNotNull().hasSize(10); }
@Test(/* expected = RuntimeException.class */ ) public void shouldReturnNullWhenFindCarWithInvalidManufacturer() { // setup test? // Use import.sql for hibernate auto-insert? Car exampleCar = CarBuilder.aCar().withManufacturer("invalid-manufacturer").build(); List<Car> foundCars = carRepository.findByExample(exampleCar); assertThat(foundCars).isEmpty(); }