BAEL-2921 Understanding getBean

This commit is contained in:
Ariel Papuga 2019-06-21 22:01:00 +02:00
parent 44fb9946c7
commit 07b49e32c2
8 changed files with 218 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package com.baeldung.getbean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
class AnnotationConfig {
@Bean(name = {"tiger", "kitty"})
@Scope(value = "prototype")
Tiger getTiger(String name) {
return new Tiger(name);
}
@Bean(name = "lion")
Lion getLion() {
return new Lion("Hardcoded lion name");
}
interface Animal {}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.getbean;
class Lion implements AnnotationConfig.Animal {
private String name;
Lion(String name) {
this.name = name;
}
String getName() {
return name;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.getbean;
class Tiger implements AnnotationConfig.Animal {
private String name;
Tiger(String name) {
this.name = name;
}
String getName() {
return name;
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameAndTypeUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenSpecifiedMatchingNameAndType_thenShouldReturnRelatedBean() {
Lion lion = context.getBean("lion", Lion.class);
assertEquals("Hardcoded lion name", lion.getName());
}
@Test
void whenSpecifiedNotMatchingNameAndType_thenShouldThrowException() {
assertThrows(BeanNotOfRequiredTypeException.class, () -> context.getBean("lion", Tiger.class));
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingBeanName_shouldReturnThatBean() {
Object requestedBean = context.getBean("lion");
assertEquals(requestedBean.getClass(), Lion.class);
}
@Test
void whenGivenNonExistingBeanName_shouldThrowException() {
assertThrows(NoSuchBeanDefinitionException.class, () -> context.getBean("non-existing"));
}
@Test
void whenCastingToWrongType_thenShouldThrowException() {
assertThrows(ClassCastException.class, () -> {
Tiger tiger = (Tiger) context.getBean("lion");
});
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByNameWithConstructorParametersUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenCorrectNameOrAlias_shouldReturnBeanWithSpecifiedName() {
Tiger tiger = (Tiger) context.getBean("tiger", "Cutie");
Tiger tigerSecond = (Tiger) context.getBean("tiger", "Striped");
Tiger tigerViaAlias = (Tiger) context.getBean("kitty", "Siberian");
assertEquals("Cutie", tiger.getName());
assertEquals("Striped", tigerSecond.getName());
assertEquals("Siberian", tigerViaAlias.getName());
}
@Test
void whenNoArgumentSpecifiedForPrototypeBean_thenShouldThrowException() {
assertThrows(UnsatisfiedDependencyException.class, () -> {
Tiger tiger = (Tiger) context.getBean("tiger");
});
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByTypeUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingUniqueType_thenShouldReturnRelatedBean() {
Lion lion = context.getBean(Lion.class);
assertNotNull(lion);
}
@Test
void whenGivenAmbiguousType_thenShouldThrowException() {
assertThrows(NoUniqueBeanDefinitionException.class, () -> context.getBean(AnnotationConfig.Animal.class));
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.getbean;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class GetBeanByTypeWithConstructorParametersUnitTest {
private ApplicationContext context;
@BeforeAll
void setup() {
context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
}
@Test
void whenGivenExistingTypeAndValidParameters_thenShouldReturnRelatedBean() {
Tiger tiger = context.getBean(Tiger.class, "Shere Khan");
assertEquals("Shere Khan", tiger.getName());
}
}