* Added test for constructor dependency injection article
* removed spring version in xml file
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
<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-3.0.xsd">
|
||||
https://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="toyota" class="com.baeldung.constructordi.domain.Car">
|
||||
<constructor-arg index="0" ref="engine" />
|
||||
<constructor-arg index="1" ref="transmission" />
|
||||
</bean>
|
||||
|
||||
<bean id="engine" class="com.baeldung.constructordi.domain.Engine">
|
||||
<bean id="engine"
|
||||
class="com.baeldung.constructordi.domain.Engine">
|
||||
<constructor-arg index="0" value="v4" />
|
||||
<constructor-arg index="1" value="2" />
|
||||
</bean>
|
||||
|
||||
<bean id="transmission" class="com.baeldung.constructordi.domain.Transmission">
|
||||
<bean id="transmission"
|
||||
class="com.baeldung.constructordi.domain.Transmission">
|
||||
<constructor-arg value="sliding" />
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.constructordi;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import com.baeldung.constructordi.domain.Car;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class)
|
||||
public class ConstructorDependencyInjectionIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenPrototypeInjection_WhenObjectFactory_ThenNewInstanceReturn() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||
Car firstContextCar = context.getBean(Car.class);
|
||||
|
||||
ApplicationContext xmlContext = new ClassPathXmlApplicationContext("constructordi.xml");
|
||||
Car secondContextCar = xmlContext.getBean(Car.class);
|
||||
|
||||
assertThat(firstContextCar).isNotSameAs(secondContextCar);
|
||||
assertThat(firstContextCar).hasToString("Engine: v8 5 Transmission: sliding");
|
||||
assertThat(secondContextCar).hasToString("Engine: v4 2 Transmission: sliding");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user