Adding jUnits for test article: Different Types of Bean Injection in Spring

This commit is contained in:
Radu Tamas 2017-10-25 13:32:02 +03:00
parent 261a16988d
commit 4ceff2d553
5 changed files with 64 additions and 5 deletions

View File

@ -8,7 +8,7 @@ public class ArticleFormatter {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml");
Article article = (Article) context.getBean("articleBean");
ArticleWithSetterInjection article = (ArticleWithSetterInjection) context.getBean("articleWithSetterInjectionBean");
String formattedArticle = article.format("This is a text !");
System.out.print(formattedArticle);

View File

@ -0,0 +1,17 @@
package com.baeldung.dependencyinjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
public class ArticleWithConstructorInjection {
private TextFormatter formatter;
@Autowired
public ArticleWithConstructorInjection(TextFormatter formatter) {
this.formatter = formatter;
}
public String format(String text) {
return formatter.format(text);
}
}

View File

@ -2,11 +2,11 @@ package com.baeldung.dependencyinjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
public class Article {
public class ArticleWithSetterInjection {
private TextFormatter formatter;
public Article(TextFormatter formatter) {
public ArticleWithSetterInjection(TextFormatter formatter) {
this.formatter = formatter;
}

View File

@ -6,11 +6,18 @@
<context:annotation-config/>
<bean id="articleBean"
class="com.baeldung.dependencyinjectiontypes.Article">
<bean id="articleWithSetterInjectionBean"
class="com.baeldung.dependencyinjectiontypes.ArticleWithSetterInjection">
<constructor-arg ref="textFormatterBean" />
</bean>
<bean id="articleWithConstructorInjectionBean"
class="com.baeldung.dependencyinjectiontypes.ArticleWithConstructorInjection">
<constructor-arg ref="textFormatterBean" />
</bean>
<bean id="textFormatterBean" class="com.baeldung.dependencyinjectiontypes.TextFormatter">
</bean>
</beans>

View File

@ -0,0 +1,35 @@
package com.baeldung.dependencyinjectiontypes;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DependencyInjectionTest {
@Test
public void givenAutowiredAnnotation_WhenSetOnSetter_ThenDependencyValid() {
ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml");
ArticleWithSetterInjection article = (ArticleWithSetterInjection) context.getBean("articleWithSetterInjectionBean");
String originalText = "This is a text !";
String formattedArticle = article.format(originalText);
assertTrue(originalText.toUpperCase().equals(formattedArticle));
}
@Test
public void givenAutowiredAnnotation_WhenSetOnConstructor_ThenDependencyValid() {
ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml");
ArticleWithConstructorInjection article = (ArticleWithConstructorInjection) context.getBean("articleWithConstructorInjectionBean");
String originalText = "This is a text !";
String formattedArticle = article.format(originalText);
assertTrue(originalText.toUpperCase().equals(formattedArticle));
}
}