RESOLVED - issue SPR-5596: Test @Autowired @Configuration class constructors
This commit is contained in:
@@ -4,19 +4,25 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import test.beans.Colour;
|
||||
import test.beans.TestBean;
|
||||
|
||||
|
||||
public class AutowiredConfigurationTests {
|
||||
public @Test
|
||||
void test() {
|
||||
|
||||
@Test
|
||||
public void testAutowiredConfigurationDependencies() {
|
||||
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
|
||||
AutowiredConfigurationTests.class.getSimpleName() + ".xml", AutowiredConfigurationTests.class);
|
||||
|
||||
@@ -26,26 +32,49 @@ public class AutowiredConfigurationTests {
|
||||
|
||||
@Configuration
|
||||
static class AutowiredConfig {
|
||||
private @Autowired
|
||||
Colour colour;
|
||||
@Autowired
|
||||
private Colour colour;
|
||||
|
||||
public @Bean
|
||||
TestBean testBean() {
|
||||
@Bean
|
||||
public TestBean testBean() {
|
||||
return new TestBean(colour.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ColorConfig {
|
||||
public @Bean
|
||||
Colour colour() {
|
||||
|
||||
@Bean
|
||||
public Colour colour() {
|
||||
return Colour.RED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Autowired} constructors are not supported on {@link Configuration} classes
|
||||
* due to CGLIB constraints
|
||||
*/
|
||||
@Test(expected=BeanCreationException.class)
|
||||
public void testAutowiredConfigurationConstructorsAreNotSupported() {
|
||||
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
|
||||
GenericApplicationContext ctx = new GenericApplicationContext(factory);
|
||||
ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
|
||||
ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
|
||||
ctx.refresh(); // should throw
|
||||
}
|
||||
|
||||
public @Test
|
||||
void testValueInjection() {
|
||||
@Configuration
|
||||
static class AutowiredConstructorConfig {
|
||||
Colour colour;
|
||||
|
||||
@Autowired
|
||||
AutowiredConstructorConfig(Colour colour) {
|
||||
this.colour = colour;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueInjection() {
|
||||
System.setProperty("myProp", "foo");
|
||||
|
||||
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
|
||||
@@ -61,8 +90,8 @@ public class AutowiredConfigurationTests {
|
||||
@Value("#{systemProperties.myProp}")
|
||||
private String name = "default";
|
||||
|
||||
public @Bean
|
||||
TestBean testBean() {
|
||||
@Bean
|
||||
public TestBean testBean() {
|
||||
return new TestBean(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
|
||||
xmlns:context="http://www.springframework.org/schema/context">
|
||||
|
||||
<context:annotation-config/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user