This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to illustrate problem described in SPR-12194
- Loading branch information
1 parent
273b956
commit c8a74d3
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>test</groupId> | ||
<artifactId>spr-12194</artifactId> | ||
<version>0.1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<spring.version>4.0.7.RELEASE</spring.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-validator</artifactId> | ||
<version>5.0.3.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.0.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
56 changes: 56 additions & 0 deletions
56
SPR-12194/src/test/java/org/springframework/issues/spr12194/ValidatorAutowiringTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.springframework.issues.spr12194; | ||
|
||
import javax.validation.Validator; | ||
|
||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.mock.web.MockServletContext; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
|
||
public class ValidatorAutowiringTest { | ||
|
||
@Test | ||
public void explicitValidator() { | ||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); | ||
context.register(MyValidatorConfiguration.class); | ||
context.setServletContext(new MockServletContext()); | ||
context.refresh(); | ||
|
||
context.getBean(Validator.class); // Fails as there are two Validator beans in the context | ||
} | ||
|
||
@Test | ||
public void noExplicitValidator() { | ||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); | ||
context.register(MyConfiguration.class); | ||
context.setServletContext(new MockServletContext()); | ||
context.refresh(); // Fails as there's no Validator bean in the context | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@ComponentScan | ||
@EnableWebMvc | ||
class MyConfiguration { | ||
|
||
} | ||
|
||
class MyValidatorConfiguration extends MyConfiguration { | ||
@Bean | ||
public Validator localValidatorFactoryBean() { | ||
return new LocalValidatorFactoryBean(); | ||
} | ||
} | ||
|
||
@Component | ||
class MyComponent { | ||
@Autowired | ||
private Validator validator; | ||
} |