Driver { public static DriverBuilder builder() { return new DriverBuilder(); } @Deprecated  Driver(); private  Driver(String name, boolean license, int age, String email, List<Car> cars); void add(Car car); @Override String toString(); static DriverBuilder builder();  }
@Test public void shouldReturnErrorWhenDriverHasNotLicense() { Driver driver = Driver.builder().withAge(20) .withCars(Arrays.asList(ferrari)) .withEmail("email@email.com") .withLicense(false) .withName("Speed Racer").build(); Set<ConstraintViolation<Driver>> validate = validator.validate(driver); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("A driver must have a license", message); }
@Test public void shouldReturnErrorWhenDriverIsYoung() { Driver driver = Driver.builder().withAge(10) .withCars(Arrays.asList(ferrari)) .withEmail("email@email.com") .withLicense(true) .withName("Speed Racer").build(); Set<ConstraintViolation<Driver>> validate = validator.validate(driver); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("Age should not be less than 18", message); }
@Test public void shouldReturnErrorWhenDriverIsOld() { Driver driver = Driver.builder().withAge(200) .withCars(Arrays.asList(ferrari)) .withEmail("email@email.com") .withLicense(true) .withName("Speed Racer").build(); Set<ConstraintViolation<Driver>> validate = validator.validate(driver); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("Age should not be greater than 150", message); }
@Test public void shouldReturnErrorWhenDriverEmailIsInvalid() { Driver driver = Driver.builder().withAge(25) .withCars(Arrays.asList(ferrari)) .withEmail("emailemail") .withLicense(true) .withName("Speed Racer").build(); Set<ConstraintViolation<Driver>> validate = validator.validate(driver); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("Email should be valid", message); }
@Test public void shouldReturnErrorWhenDriverNotHaveCar() { Driver driver = Driver.builder().withAge(25) .withEmail("email@email.com") .withLicense(true) .withName("Speed Racer").build(); Set<ConstraintViolation<Driver>> validate = validator.validate(driver); assertFalse(validate.isEmpty()); String message = validate.stream().findFirst() .map(ConstraintViolation::getMessageTemplate) .orElse(null); assertEquals("It must have one car at least", message); }
@Test public void shouldCreateInstance() { Driver driver = Driver.builder().withAge(25) .withCars(Arrays.asList(ferrari)) .withEmail("email@email.com") .withLicense(true) .withName("Speed Racer").build(); Set<ConstraintViolation<Driver>> validate = validator.validate(driver); assertTrue(validate.isEmpty()); }