|
15 | 15 | import jakarta.validation.constraints.Positive; |
16 | 16 | import jakarta.validation.constraints.PositiveOrZero; |
17 | 17 | import lombok.With; |
| 18 | +import org.junit.jupiter.api.Nested; |
18 | 19 | import org.junit.jupiter.api.Test; |
19 | 20 | import org.springframework.lang.Nullable; |
| 21 | +import org.springframework.validation.annotation.Validated; |
20 | 22 |
|
21 | 23 | import java.math.BigDecimal; |
22 | 24 | import java.math.BigInteger; |
|
32 | 34 | import java.time.temporal.ChronoUnit; |
33 | 35 |
|
34 | 36 | import static it.aboutbits.springboot.testing.validation.ValidationAssertTest.TestValidationAssert.assertThatValidation; |
| 37 | +import static org.assertj.core.api.Assertions.assertThatCode; |
35 | 38 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
36 | 39 |
|
37 | 40 | class ValidationAssertTest { |
@@ -524,6 +527,97 @@ void propertyMissingRule_shouldFail() { |
524 | 527 | .isCompliant()); |
525 | 528 | } |
526 | 529 |
|
| 530 | + @Nested |
| 531 | + @SuppressWarnings("java:S5778") // Suppress the warning for lambdas with more than one exception cause |
| 532 | + class AnnotationChecking { |
| 533 | + @Test |
| 534 | + void givenValidatedClass_andValidParameter_shouldSucceed() { |
| 535 | + assertThatCode( |
| 536 | + () -> assertThatValidation().calling( |
| 537 | + ClassWithValidated.class, |
| 538 | + "someMethodWithValidParameter", |
| 539 | + String.class |
| 540 | + ).isEnabled() |
| 541 | + ).doesNotThrowAnyException(); |
| 542 | + |
| 543 | + assertThatCode( |
| 544 | + () -> assertThatValidation().calling( |
| 545 | + ClassWithValidated.class, |
| 546 | + "someMethodWithValidParameter", |
| 547 | + Long.class, |
| 548 | + String.class |
| 549 | + ).isEnabled() |
| 550 | + ).doesNotThrowAnyException(); |
| 551 | + |
| 552 | + assertThatCode( |
| 553 | + () -> assertThatValidation().calling( |
| 554 | + ClassWithValidated.class, |
| 555 | + "someMethodWithValidParameter", |
| 556 | + Long.class, |
| 557 | + Integer.class, |
| 558 | + String.class |
| 559 | + ).isEnabled() |
| 560 | + ).doesNotThrowAnyException(); |
| 561 | + } |
| 562 | + |
| 563 | + @Test |
| 564 | + void givenNotValidatedClass_shouldAlwaysFail() { |
| 565 | + assertThatExceptionOfType(AssertionError.class).isThrownBy( |
| 566 | + () -> assertThatValidation().calling( |
| 567 | + ClassWithoutValidated.class, |
| 568 | + "someMethodWithValidParameter", |
| 569 | + String.class |
| 570 | + ).isEnabled() |
| 571 | + ); |
| 572 | + |
| 573 | + assertThatExceptionOfType(AssertionError.class).isThrownBy( |
| 574 | + () -> assertThatValidation().calling( |
| 575 | + ClassWithoutValidated.class, |
| 576 | + "someMethodWithValidParameter", |
| 577 | + Long.class, |
| 578 | + String.class |
| 579 | + ).isEnabled() |
| 580 | + ); |
| 581 | + |
| 582 | + assertThatExceptionOfType(AssertionError.class).isThrownBy( |
| 583 | + () -> assertThatValidation().calling( |
| 584 | + ClassWithoutValidated.class, |
| 585 | + "someMethodWithValidParameter", |
| 586 | + Long.class, |
| 587 | + Integer.class, |
| 588 | + String.class |
| 589 | + ).isEnabled() |
| 590 | + ); |
| 591 | + |
| 592 | + assertThatExceptionOfType(AssertionError.class).isThrownBy( |
| 593 | + () -> assertThatValidation().calling( |
| 594 | + ClassWithoutValidated.class, |
| 595 | + "someMethodWithoutValidParameter", |
| 596 | + String.class |
| 597 | + ).isEnabled() |
| 598 | + ); |
| 599 | + |
| 600 | + assertThatExceptionOfType(AssertionError.class).isThrownBy( |
| 601 | + () -> assertThatValidation().calling( |
| 602 | + ClassWithoutValidated.class, |
| 603 | + "someMethodWithoutValidParameter", |
| 604 | + Long.class, |
| 605 | + String.class |
| 606 | + ).isEnabled() |
| 607 | + ); |
| 608 | + |
| 609 | + assertThatExceptionOfType(AssertionError.class).isThrownBy( |
| 610 | + () -> assertThatValidation().calling( |
| 611 | + ClassWithoutValidated.class, |
| 612 | + "someMethodWithoutValidParameter", |
| 613 | + Long.class, |
| 614 | + Integer.class, |
| 615 | + String.class |
| 616 | + ).isEnabled() |
| 617 | + ); |
| 618 | + } |
| 619 | + } |
| 620 | + |
527 | 621 | private static SomeValidParameter getSomeValidParameter() { |
528 | 622 | return new SomeValidParameter( |
529 | 623 | // NotNull |
@@ -654,4 +748,47 @@ public static TestValidationAssert assertThatValidation() { |
654 | 748 | public static final class TestRuleBuilder extends BaseRuleBuilder<TestRuleBuilder> { |
655 | 749 | } |
656 | 750 | } |
| 751 | + |
| 752 | + @SuppressWarnings({"java:S1186", "unused"}) // Suppress the "empty method body" warning |
| 753 | + @Validated |
| 754 | + public static class ClassWithValidated { |
| 755 | + public void someMethodWithValidParameter(@Valid String last) { |
| 756 | + } |
| 757 | + |
| 758 | + public void someMethodWithValidParameter(Long first, @Valid String last) { |
| 759 | + } |
| 760 | + |
| 761 | + public void someMethodWithValidParameter(Long first, Integer second, @Valid String last) { |
| 762 | + } |
| 763 | + |
| 764 | + public void someMethodWithoutValidParameter(String last) { |
| 765 | + } |
| 766 | + |
| 767 | + public void someMethodWithoutValidParameter(Long first, String last) { |
| 768 | + } |
| 769 | + |
| 770 | + public void someMethodWithoutValidParameter(Long first, Integer second, String last) { |
| 771 | + } |
| 772 | + } |
| 773 | + |
| 774 | + @SuppressWarnings({"java:S1186", "unused"}) // Suppress the "empty method body" warning |
| 775 | + public static class ClassWithoutValidated { |
| 776 | + public void someMethodWithValidParameter(@Valid String last) { |
| 777 | + } |
| 778 | + |
| 779 | + public void someMethodWithValidParameter(Long first, @Valid String last) { |
| 780 | + } |
| 781 | + |
| 782 | + public void someMethodWithValidParameter(Long first, Integer second, @Valid String last) { |
| 783 | + } |
| 784 | + |
| 785 | + public void someMethodWithoutValidParameter(String last) { |
| 786 | + } |
| 787 | + |
| 788 | + public void someMethodWithoutValidParameter(Long first, String last) { |
| 789 | + } |
| 790 | + |
| 791 | + public void someMethodWithoutValidParameter(Long first, Integer second, String last) { |
| 792 | + } |
| 793 | + } |
657 | 794 | } |
0 commit comments