From 5eaa0281230916d44a671e1a7428cc1f6054344c Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Thu, 13 Jun 2019 13:10:54 -0300 Subject: [PATCH] * Added test for constructor dependency injection article * removed spring version in xml file --- spring-core/pom.xml | 7 ++++ .../src/main/resources/constructordi.xml | 8 +++-- ...torDependencyInjectionIntegrationTest.java | 33 +++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 spring-core/src/test/java/com/baeldung/constructordi/ConstructorDependencyInjectionIntegrationTest.java diff --git a/spring-core/pom.xml b/spring-core/pom.xml index 814addecdd..75e9fd7131 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -56,6 +56,12 @@ spring-boot-test ${mockito.spring.boot.version} test + + + org.assertj + assertj-core + ${assertj.version} + test commons-io @@ -85,6 +91,7 @@ 2.5 1.5.2.RELEASE 1.10.19 + 3.12.2 \ No newline at end of file diff --git a/spring-core/src/main/resources/constructordi.xml b/spring-core/src/main/resources/constructordi.xml index 231e72adcb..983a00a80f 100644 --- a/spring-core/src/main/resources/constructordi.xml +++ b/spring-core/src/main/resources/constructordi.xml @@ -1,19 +1,21 @@ + https://www.springframework.org/schema/beans/spring-beans.xsd"> - + - + diff --git a/spring-core/src/test/java/com/baeldung/constructordi/ConstructorDependencyInjectionIntegrationTest.java b/spring-core/src/test/java/com/baeldung/constructordi/ConstructorDependencyInjectionIntegrationTest.java new file mode 100644 index 0000000000..7bd0ad0c86 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/constructordi/ConstructorDependencyInjectionIntegrationTest.java @@ -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"); + } + +}