|
|
|
|
@ -27,6 +27,7 @@ import org.junit.Test;
@@ -27,6 +27,7 @@ import org.junit.Test;
|
|
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
|
|
|
|
|
|
import org.springframework.beans.DirectFieldAccessor; |
|
|
|
|
import org.springframework.boot.test.util.EnvironmentTestUtils; |
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
@ -75,6 +76,19 @@ public class ValidationAutoConfigurationTests {
@@ -75,6 +76,19 @@ public class ValidationAutoConfigurationTests {
|
|
|
|
|
service.doSomething(2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void validationCanBeConfiguredToUseJdkProxy() { |
|
|
|
|
load(AnotherSampleServiceConfiguration.class, "spring.aop.proxy-target-class=false"); |
|
|
|
|
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1); |
|
|
|
|
assertThat(this.context.getBeansOfType(DefaultAnotherSampleService.class)) |
|
|
|
|
.isEmpty(); |
|
|
|
|
AnotherSampleService service = this.context |
|
|
|
|
.getBean(AnotherSampleService.class); |
|
|
|
|
service.doSomething(42); |
|
|
|
|
this.thrown.expect(ConstraintViolationException.class); |
|
|
|
|
service.doSomething(2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void userDefinedMethodValidationPostProcessorTakesPrecedence() { |
|
|
|
|
load(SampleConfiguration.class); |
|
|
|
|
@ -90,14 +104,15 @@ public class ValidationAutoConfigurationTests {
@@ -90,14 +104,15 @@ public class ValidationAutoConfigurationTests {
|
|
|
|
|
.getPropertyValue("validator")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void load(Class<?> config) { |
|
|
|
|
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); |
|
|
|
|
public void load(Class<?> config, String... environment) { |
|
|
|
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
|
|
|
|
EnvironmentTestUtils.addEnvironment(ctx, environment); |
|
|
|
|
if (config != null) { |
|
|
|
|
applicationContext.register(config); |
|
|
|
|
ctx.register(config); |
|
|
|
|
} |
|
|
|
|
applicationContext.register(ValidationAutoConfiguration.class); |
|
|
|
|
applicationContext.refresh(); |
|
|
|
|
this.context = applicationContext; |
|
|
|
|
ctx.register(ValidationAutoConfiguration.class); |
|
|
|
|
ctx.refresh(); |
|
|
|
|
this.context = ctx; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Validated |
|
|
|
|
@ -123,6 +138,16 @@ public class ValidationAutoConfigurationTests {
@@ -123,6 +138,16 @@ public class ValidationAutoConfigurationTests {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class AnotherSampleServiceConfiguration { |
|
|
|
|
|
|
|
|
|
@Bean |
|
|
|
|
public AnotherSampleService anotherSampleService() { |
|
|
|
|
return new DefaultAnotherSampleService(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
|
static class SampleConfiguration { |
|
|
|
|
|
|
|
|
|
|