added tests & changed configuration to Java-based config

This commit is contained in:
Ahmed Tawila 2017-06-16 13:57:37 +02:00
parent 9f87819fc7
commit ac2ce9636c
6 changed files with 101 additions and 9 deletions

View File

@ -1,15 +1,16 @@
package com.baeldung;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baeldung.configuration.ConstructorConfig;
import com.baeldung.model.Student;
public class ConstructorApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("constructor-context.xml");
Student student = (Student) context.getBean("student");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConstructorConfig.class);
Student student = context.getBean(Student.class);
student.introduceMyself();
}
}

View File

@ -1,15 +1,13 @@
package com.baeldung;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.configuration.SetterConfig;
import com.baeldung.model.Student;
public class SetterApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("setter-context.xml");
Student student = (Student) context.getBean("student");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SetterConfig.class);
Student student = context.getBean(Student.class);
student.introduceMyself();
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.model.College;
import com.baeldung.model.Student;
@Configuration
public class ConstructorConfig {
@Bean
public Student student() {
return new Student(1, "John", college());
}
@Bean
public College college() {
return new College(1, "New York");
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.model.College;
import com.baeldung.model.Student;
@Configuration
public class SetterConfig {
@Bean
public Student student() {
Student student = new Student();
student.setId(2);
student.setName("Tom");
student.setCollege(college());
return student;
}
@Bean
public College college() {
College college = new College();
college.setId(2);
college.setName("Pittsburgh");
return college;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.configuration;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.model.Student;
public class ConstructorConfigTest {
@Test
public void testConstructorDependencyInjection() {
ApplicationContext context = new AnnotationConfigApplicationContext(ConstructorConfig.class);
Student student = context.getBean(Student.class);
assertEquals(1, student.getId());
assertEquals("John", student.getName());
assertEquals(1, student.getCollege().getId());
assertEquals("New York", student.getCollege().getName());
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.configuration;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.model.Student;
public class SetterConfigTest {
@Test
public void testConstructorDependencyInjection() {
ApplicationContext context = new AnnotationConfigApplicationContext(SetterConfig.class);
Student student = context.getBean(Student.class);
assertEquals(2, student.getId());
assertEquals("Tom", student.getName());
assertEquals(2, student.getCollege().getId());
assertEquals("Pittsburgh", student.getCollege().getName());
}
}