BAEL-4952: Fix code examples for the BigDecimal Validation (#10751)
Co-authored-by: Krzysztof Woyke <krzysztof.woyke.sp@lhsystems.com>
This commit is contained in:
parent
d3247f2919
commit
07051eb8d8
@ -1,18 +1,17 @@
|
|||||||
package com.baeldung.javaxval.bigdecimal;
|
package com.baeldung.javaxval.bigdecimal;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.validation.ConstraintViolation;
|
|
||||||
import javax.validation.Validation;
|
|
||||||
import javax.validation.Validator;
|
|
||||||
|
|
||||||
import com.baeldung.javaxval.LocaleAwareUnitTest;
|
import com.baeldung.javaxval.LocaleAwareUnitTest;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class InvoiceUnitTest extends LocaleAwareUnitTest {
|
public class InvoiceUnitTest extends LocaleAwareUnitTest {
|
||||||
|
|
||||||
private static Validator validator;
|
private static Validator validator;
|
||||||
@ -24,41 +23,67 @@ public class InvoiceUnitTest extends LocaleAwareUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPriceIntegerDigitLessThanThreeWithDecimalValue_thenShouldGiveConstraintViolations() {
|
public void whenLessThanThreeIntegerDigits_thenShouldNotGiveConstraintViolations() {
|
||||||
Invoice invoice = new Invoice(new BigDecimal(10.21), "Book purchased");
|
Invoice invoice = new Invoice(new BigDecimal("10.21"), "Book purchased");
|
||||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
assertThat(violations.size()).isEqualTo(1);
|
assertThat(violations).isEmpty();
|
||||||
violations.forEach(action -> assertThat(action.getMessage()).isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPriceIntegerDigitLessThanThreeWithIntegerValue_thenShouldNotGiveConstraintViolations() {
|
public void whenThreeIntegerDigits_thenShouldNotGiveConstraintViolations() {
|
||||||
Invoice invoice = new Invoice(new BigDecimal(10), "Book purchased");
|
Invoice invoice = new Invoice(new BigDecimal("102.21"), "Book purchased");
|
||||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
assertThat(violations.size()).isEqualTo(0);
|
assertThat(violations).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPriceIntegerDigitGreaterThanThree_thenShouldGiveConstraintViolations() {
|
public void whenMoreThanThreeIntegerDigits_thenShouldGiveConstraintViolations() {
|
||||||
Invoice invoice = new Invoice(new BigDecimal(1021.21), "Book purchased");
|
Invoice invoice = new Invoice(new BigDecimal("1021.21"), "Book purchased");
|
||||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
assertThat(violations.size()).isEqualTo(1);
|
assertThat(violations).hasSize(1);
|
||||||
violations.forEach(action -> assertThat(action.getMessage()).isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
|
assertThat(violations)
|
||||||
|
.extracting("message")
|
||||||
|
.containsOnly("numeric value out of bounds (<3 digits>.<2 digits> expected)");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLessThanTwoFractionDigits_thenShouldNotGiveConstraintViolations() {
|
||||||
|
Invoice invoice = new Invoice(new BigDecimal("99.9"), "Book purchased");
|
||||||
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
|
assertThat(violations).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTwoFractionDigits_thenShouldNotGiveConstraintViolations() {
|
||||||
|
Invoice invoice = new Invoice(new BigDecimal("99.99"), "Book purchased");
|
||||||
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
|
assertThat(violations).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMoreThanTwoFractionDigits_thenShouldGiveConstraintViolations() {
|
||||||
|
Invoice invoice = new Invoice(new BigDecimal("99.999"), "Book purchased");
|
||||||
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
|
assertThat(violations).hasSize(1);
|
||||||
|
assertThat(violations)
|
||||||
|
.extracting("message")
|
||||||
|
.containsOnly("numeric value out of bounds (<3 digits>.<2 digits> expected)");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPriceIsZero_thenShouldGiveConstraintViolations() {
|
public void whenPriceIsZero_thenShouldGiveConstraintViolations() {
|
||||||
Invoice invoice = new Invoice(new BigDecimal(000.00), "Book purchased");
|
Invoice invoice = new Invoice(new BigDecimal("0.00"), "Book purchased");
|
||||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
assertThat(violations.size()).isEqualTo(1);
|
assertThat(violations).hasSize(1);
|
||||||
violations.forEach(action -> assertThat(action.getMessage()).isEqualTo("must be greater than 0.0"));
|
assertThat(violations)
|
||||||
|
.extracting("message")
|
||||||
|
.containsOnly("must be greater than 0.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPriceIsGreaterThanZero_thenShouldNotGiveConstraintViolations() {
|
public void whenPriceIsGreaterThanZero_thenShouldNotGiveConstraintViolations() {
|
||||||
Invoice invoice = new Invoice(new BigDecimal(100.50), "Book purchased");
|
Invoice invoice = new Invoice(new BigDecimal("100.50"), "Book purchased");
|
||||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||||
assertThat(violations.size()).isEqualTo(0);
|
assertThat(violations).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user