From 07b49e32c2ef1a65328482e1a251d1349485e219 Mon Sep 17 00:00:00 2001 From: Ariel Papuga Date: Fri, 21 Jun 2019 22:01:00 +0200 Subject: [PATCH] BAEL-2921 Understanding getBean --- .../baeldung/getbean/AnnotationConfig.java | 22 ++++++++++ .../main/java/com/baeldung/getbean/Lion.java | 13 ++++++ .../main/java/com/baeldung/getbean/Tiger.java | 13 ++++++ .../getbean/GetBeanByNameAndTypeUnitTest.java | 33 +++++++++++++++ .../getbean/GetBeanByNameUnitTest.java | 40 +++++++++++++++++++ ...NameWithConstructorParametersUnitTest.java | 38 ++++++++++++++++++ .../getbean/GetBeanByTypeUnitTest.java | 33 +++++++++++++++ ...TypeWithConstructorParametersUnitTest.java | 26 ++++++++++++ 8 files changed, 218 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/getbean/AnnotationConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/getbean/Lion.java create mode 100644 spring-core/src/main/java/com/baeldung/getbean/Tiger.java create mode 100644 spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java create mode 100644 spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java create mode 100644 spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java create mode 100644 spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java create mode 100644 spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java diff --git a/spring-core/src/main/java/com/baeldung/getbean/AnnotationConfig.java b/spring-core/src/main/java/com/baeldung/getbean/AnnotationConfig.java new file mode 100644 index 0000000000..12e3ab55e3 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/getbean/AnnotationConfig.java @@ -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 {} +} diff --git a/spring-core/src/main/java/com/baeldung/getbean/Lion.java b/spring-core/src/main/java/com/baeldung/getbean/Lion.java new file mode 100644 index 0000000000..96c569aeb9 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/getbean/Lion.java @@ -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; + } +} diff --git a/spring-core/src/main/java/com/baeldung/getbean/Tiger.java b/spring-core/src/main/java/com/baeldung/getbean/Tiger.java new file mode 100644 index 0000000000..85b9626c79 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/getbean/Tiger.java @@ -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; + } +} diff --git a/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java new file mode 100644 index 0000000000..e06804c28e --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameAndTypeUnitTest.java @@ -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)); + } +} diff --git a/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java new file mode 100644 index 0000000000..70a135b314 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameUnitTest.java @@ -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"); + }); + } +} diff --git a/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java new file mode 100644 index 0000000000..4ac8c1ac13 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByNameWithConstructorParametersUnitTest.java @@ -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"); + }); + } +} diff --git a/spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java new file mode 100644 index 0000000000..a4d4ab732b --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeUnitTest.java @@ -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)); + } +} diff --git a/spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java new file mode 100644 index 0000000000..08bcb92145 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/getbean/GetBeanByTypeWithConstructorParametersUnitTest.java @@ -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()); + } +}