BAEL-5642 Using @NotNull as a method parameter (#12483)
* BAEL-5642 Using @NotNull as a method parameter * BAEL-5642 Add Spring Boot and bump spring and hibernate-validator versions
This commit is contained in:
parent
09651da81e
commit
e65ce930bb
|
@ -34,6 +34,17 @@
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-test</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<!-- uncomment in order to enable Hibernate Validator Anotation Processor -->
|
<!-- uncomment in order to enable Hibernate Validator Anotation Processor -->
|
||||||
|
@ -46,13 +57,14 @@
|
||||||
</plugin> </plugins> </build> -->
|
</plugin> </plugins> </build> -->
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
|
<hibernate-validator.version>6.2.3.Final</hibernate-validator.version>
|
||||||
<hibernate-validator.ap.version>6.2.0.Final</hibernate-validator.ap.version>
|
<hibernate-validator.ap.version>6.2.0.Final</hibernate-validator.ap.version>
|
||||||
<maven.compiler.version>3.6.1</maven.compiler.version>
|
<maven.compiler.version>3.6.1</maven.compiler.version>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<javax.el.version>3.0.0</javax.el.version>
|
<javax.el.version>3.0.0</javax.el.version>
|
||||||
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
<org.springframework.version>5.3.21</org.springframework.version>
|
||||||
|
<spring.boot.version>2.7.1</spring.boot.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.javaxval.notnull;
|
||||||
|
|
||||||
|
import javax.validation.Validation;
|
||||||
|
import javax.validation.Validator;
|
||||||
|
import javax.validation.ValidatorFactory;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class NotNullMethodParameter {
|
||||||
|
|
||||||
|
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||||
|
Validator validator = factory.getValidator();
|
||||||
|
|
||||||
|
public int doesNotValidateNotNull(@NotNull String myString) {
|
||||||
|
return myString.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int validateNotNull(@NotNull String myString) {
|
||||||
|
validator.validate(myString);
|
||||||
|
return myString.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.javaxval.notnull;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Validated
|
||||||
|
public class ValidatingComponent {
|
||||||
|
|
||||||
|
public int validateNotNull(@NotNull String data)
|
||||||
|
{
|
||||||
|
return data.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int callAnnotatedMethod(String data) {
|
||||||
|
return validateNotNull(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.javaxval.notnull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
class NotNullMethodParameterUnitTest {
|
||||||
|
|
||||||
|
private NotNullMethodParameter demo = new NotNullMethodParameter();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNull_whenInvokedwithNoValidator_thenNullPointerException() {
|
||||||
|
assertThrows(NullPointerException.class, () -> demo.doesNotValidateNotNull(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNull_whenInvokedWithValidator_thenIllegalArgumentException() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> demo.validateNotNull(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.javaxval.notnull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintViolation;
|
||||||
|
import javax.validation.ConstraintViolationException;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class ValidatingComponentIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired ValidatingComponent component;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenValue_whenValidate_thenSuccess() {
|
||||||
|
assertThat(component.validateNotNull("Not null!"), is(9));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNull_whenValidate_thenConstraintViolationException() {
|
||||||
|
ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> component.validateNotNull(null));
|
||||||
|
Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
|
||||||
|
assertThat(constraintViolations.iterator().next().getConstraintDescriptor().getAnnotation().annotationType(), is(NotNull.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenNull_whenOnlyCalledMethodHasAnnotation_thenNoValidation() {
|
||||||
|
assertThrows(NullPointerException.class, () -> component.callAnnotatedMethod(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
static class TestApplication {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue