Czemu wbudowany walidator PESELA do Hibernate źle waliduje?

0

dokumentacja: https://docs.jboss.org/hibernate/validator/5.4/api/org/hibernate/validator/internal/constraintvalidators/hv/pl/PESELValidator.html

przykłądowy poprawny pesel: 94092367705
do sprawdzenia tutaj: https://sprawdz-numer.com/pesel

a poniższy test się wywala:

@Test
    public void testEncryptingPesel() {
        // given
        CharSequence validPesel = "94092367705";

        // when
        PESELValidator validator = new PESELValidator();

        // then
        assertTrue(validator.isValid(validPesel, null));
}
0

trzeba dołożyć:

validator.initialize(null);

cholera wie po co.

@Test
    public void testEncryptingPesel() {
        // given
        CharSequence validPesel = "94092367705";

        // when

        PESELValidator validator = new PESELValidator();
        validator.initialize(null);

        // then
        assertTrue(validator.isValid(validPesel, null));
}
0
Julian_ napisał(a):

dokumentacja: https://docs.jboss.org/hibernate/validator/5.4/api/org/hibernate/validator/internal/constraintvalidators/hv/pl/PESELValidator.html

przykłądowy poprawny pesel: 94092367705
do sprawdzenia tutaj: https://sprawdz-numer.com/pesel

a poniższy test się wywala:

@Test
    public void testEncryptingPesel() {
        // given
        CharSequence validPesel = "94092367705";

        // when
        PESELValidator validator = new PESELValidator();

        // then
        assertTrue(validator.isValid(validPesel, null));
}

spróbuj dodać przed wywołaniem isValid:

    validator.initialize(null);
0

Najlepiej testuj to tak jak oni czyli:

0

Co za bezsens, link sie nie dodał: link

0

tak też testowałem, ale teraz chciałem przetestować szyfrowanie pesela:

@Entity
public abstract class Employee {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@PESEL
	@Transient
	private final String pesel; // TODO encrypt and validate

	@Column(unique = true, updatable = false)
	public final String peselEncrypted; // TODO encrypt and validate


		public Employee(String pesel) {
		this.pesel = pesel;
		this.peselEncrypted = HashMD5.getHashed(pesel);
	}
// ...
public class EncryptingTest {

	PESELValidator peselValidator;

	@Before
	public void onStart2() {
		this.peselValidator = new PESELValidator();
		peselValidator.initialize(null);
	}

	@Test
	public void testEncryptingPesel() {
		// given
		CharSequence validPesel = "94092367705";

		// when
		Employee emp = new Shipper(validPesel.toString());

		// then
		assertTrue(peselValidator.isValid(validPesel, null));
		assertTrue(!peselValidator.isValid(emp.peselEncrypted, null));
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1