Created
July 16, 2019 14:10
-
-
Save t3hk0d3/f0abb5ae09ae48dee36030fbb5e9f294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import org.unbrokendome.jackson.beanvalidation.BeanValidationModule; | |
import org.unbrokendome.jackson.beanvalidation.JsonValidated; | |
import javax.validation.ConstraintViolationException; | |
import javax.validation.Validation; | |
import javax.validation.ValidatorFactory; | |
import javax.validation.constraints.NotNull; | |
class BeanValidationTest { | |
@Test | |
void testBeanValidation() throws Exception { | |
String invalidJson = "{\"not_null\": null}"; | |
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); | |
var objectMapper = new ObjectMapper() | |
.registerModule(new BeanValidationModule(validatorFactory)) | |
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); | |
Assertions.assertThrows(ConstraintViolationException.class, () -> { | |
objectMapper.readValue(invalidJson, TestSerializable.class); | |
}); | |
} | |
@JsonValidated | |
public static class TestSerializable { | |
@NotNull | |
private final String notNull; | |
@JsonCreator | |
public TestSerializable( | |
@JsonProperty(value = "not_null", required = true) String notNull | |
) { | |
this.notNull = notNull; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment