evaluation article, different types of bean injection. (#3511)

* Different types of bean injection in spring, initial write-up

* Evaluation article code example, different types of bean injection.
This commit is contained in:
Syed Munawwer Ali
2018-02-24 21:51:16 +05:30
committed by Grzegorz Piwowarek
parent 83d5f6a485
commit 54bd8a5cdd
5 changed files with 142 additions and 36 deletions
@@ -2,13 +2,25 @@ package com.baeldung.dependencyinjectiontypes;
import static org.junit.Assert.assertTrue;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=com.baeldung.setterdi.Config.class, loader=AnnotationConfigContextLoader.class)
public class DependencyInjectionTest {
@Autowired
private ApplicationContext appContext;
Logger logger = Logger.getLogger(this.getClass());
@Test
/* @Test
public void givenAutowiredAnnotation_WhenSetOnSetter_ThenDependencyValid() {
ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml");
@@ -30,6 +42,20 @@ public class DependencyInjectionTest {
String formattedArticle = article.format(originalText);
assertTrue(originalText.toUpperCase().equals(formattedArticle));
}*/
@Test
public void givenAutowiredAnnotation_OnSetter_ThenDependencyValid() {
Student student = (Student) appContext.getBean("student");
String teacherFound = student.getTeacher();
assertTrue(teacherFound.equals("author"));
}
@Test
public void givenAutowiredAnnotation_OnConstructor_ThenDependencyValid() {
Student2 student2 = (Student2) appContext.getBean("student2");
String teacherFound = student2.getTeacher();
assertTrue(teacherFound.equals("author"));
}
}