* Added test for constructor dependency injection article

* removed spring version in xml file
This commit is contained in:
Gerardo Roza 2019-06-13 13:10:54 -03:00
parent a057010fed
commit 5eaa028123
3 changed files with 45 additions and 3 deletions

View File

@ -56,6 +56,12 @@
<artifactId>spring-boot-test</artifactId>
<version>${mockito.spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
@ -85,6 +91,7 @@
<commons.io.version>2.5</commons.io.version>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
<mockito.version>1.10.19</mockito.version>
<assertj.version>3.12.2</assertj.version>
</properties>
</project>

View File

@ -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>

View File

@ -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");
}
}