Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Add tests to illustrate problem described in SPR-12194
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Sep 15, 2014
1 parent 273b956 commit c8a74d3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
42 changes: 42 additions & 0 deletions SPR-12194/pom.xml
@@ -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>
@@ -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;
}

0 comments on commit c8a74d3

Please sign in to comment.