diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index 5cd98cf8caef..1368f39fb771 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -108,9 +108,9 @@ void close() { @Test void incompleteBeanDefinition() { bf.registerBeanDefinition("testBean", new GenericBeanDefinition()); - assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> - bf.getBean("testBean")) - .withRootCauseInstanceOf(IllegalStateException.class); + assertThatExceptionOfType(BeanCreationException.class) + .isThrownBy(() -> bf.getBean("testBean")) + .withRootCauseInstanceOf(IllegalStateException.class); } @Test @@ -121,11 +121,11 @@ void resourceInjection() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); + ResourceInjectionBean bean = bf.getBean("annotatedBean", ResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); - bean = (ResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); } @@ -140,12 +140,13 @@ void resourceInjectionWithNullBean() { tb.setFactoryMethodName("createTestBean"); bf.registerBeanDefinition("testBean", tb); - NonPublicResourceInjectionBean bean = (NonPublicResourceInjectionBean) bf.getBean("annotatedBean"); + @SuppressWarnings("rawtypes") + NonPublicResourceInjectionBean bean = bf.getBean("annotatedBean", NonPublicResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); - bean = (NonPublicResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", NonPublicResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); @@ -214,7 +215,7 @@ void extendedResourceInjection() { NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); - TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); + TypedExtendedResourceInjectionBean bean = bf.getBean("annotatedBean", TypedExtendedResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -222,7 +223,7 @@ void extendedResourceInjection() { assertThat(bean.getNestedTestBean()).isSameAs(ntb); assertThat(bean.getBeanFactory()).isSameAs(bf); - bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", TypedExtendedResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -244,7 +245,7 @@ void extendedResourceInjectionWithDestruction() { bf.registerSingleton("nestedTestBean", ntb); TestBean tb = bf.getBean("testBean", TestBean.class); - TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); + TypedExtendedResourceInjectionBean bean = bf.getBean("annotatedBean", TypedExtendedResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -271,7 +272,7 @@ void extendedResourceInjectionWithOverriding() { NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); - TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean"); + TypedExtendedResourceInjectionBean bean = bf.getBean("annotatedBean", TypedExtendedResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb2); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -289,7 +290,7 @@ void extendedResourceInjectionWithSkippedOverriddenMethods() { NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); - OverriddenExtendedResourceInjectionBean bean = (OverriddenExtendedResourceInjectionBean) bf.getBean("annotatedBean"); + OverriddenExtendedResourceInjectionBean bean = bf.getBean("annotatedBean", OverriddenExtendedResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -309,7 +310,7 @@ void extendedResourceInjectionWithDefaultMethod() { NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); - DefaultMethodResourceInjectionBean bean = (DefaultMethodResourceInjectionBean) bf.getBean("annotatedBean"); + DefaultMethodResourceInjectionBean bean = bf.getBean("annotatedBean", DefaultMethodResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -332,7 +333,7 @@ void optionalResourceInjection() { NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -360,7 +361,7 @@ void optionalResourceInjectionWithSingletonRemoval() { NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -375,7 +376,7 @@ void optionalResourceInjectionWithSingletonRemoval() { bf.destroySingleton("testBean"); - bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); @@ -390,7 +391,7 @@ void optionalResourceInjectionWithSingletonRemoval() { bf.registerSingleton("testBean", tb); - bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -417,7 +418,7 @@ void optionalResourceInjectionWithBeanDefinitionRemoval() { NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); assertThat(bean.getTestBean2()).isSameAs(bf.getBean("testBean")); assertThat(bean.getTestBean3()).isSameAs(bf.getBean("testBean")); @@ -432,7 +433,7 @@ void optionalResourceInjectionWithBeanDefinitionRemoval() { bf.removeBeanDefinition("testBean"); - bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); @@ -447,7 +448,7 @@ void optionalResourceInjectionWithBeanDefinitionRemoval() { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); - bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); assertThat(bean.getTestBean2()).isSameAs(bf.getBean("testBean")); assertThat(bean.getTestBean3()).isSameAs(bf.getBean("testBean")); @@ -476,8 +477,8 @@ void optionalCollectionResourceInjection() { bf.registerSingleton("nestedTestBean2", ntb2); // Two calls to verify that caching doesn't break re-creation. - OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); + bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -507,8 +508,8 @@ void optionalCollectionResourceInjectionWithSingleElement() { bf.registerSingleton("nestedTestBean1", ntb1); // Two calls to verify that caching doesn't break re-creation. - OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); + bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -528,7 +529,7 @@ void optionalResourceInjectionWithIncompleteDependencies() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -540,7 +541,7 @@ void optionalResourceInjectionWithIncompleteDependencies() { void optionalResourceInjectionWithNoDependencies() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); @@ -562,7 +563,7 @@ void orderedResourceInjection() { ntb2.setOrder(1); bf.registerSingleton("nestedTestBean2", ntb2); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -588,7 +589,7 @@ void annotationOrderedResourceInjection() { FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -619,8 +620,8 @@ void orderedCollectionResourceInjection() { bf.registerSingleton("nestedTestBean2", ntb2); // Two calls to verify that caching doesn't break re-creation. - OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); + bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -652,8 +653,8 @@ void annotationOrderedCollectionResourceInjection() { bf.registerSingleton("nestedTestBean2", ntb2); // Two calls to verify that caching doesn't break re-creation. - OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); - bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + OptionalCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); + bean = bf.getBean("annotatedBean", OptionalCollectionResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -680,7 +681,7 @@ void constructorResourceInjection() { NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); - ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -688,7 +689,7 @@ void constructorResourceInjection() { assertThat(bean.getNestedTestBean()).isSameAs(ntb); assertThat(bean.getBeanFactory()).isSameAs(bf); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -707,7 +708,7 @@ void constructorResourceInjectionWithSingletonRemoval() { NestedTestBean ntb = new NestedTestBean(); bf.registerSingleton("nestedTestBean", ntb); - ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -717,7 +718,7 @@ void constructorResourceInjectionWithSingletonRemoval() { bf.destroySingleton("nestedTestBean"); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -727,7 +728,7 @@ void constructorResourceInjectionWithSingletonRemoval() { bf.registerSingleton("nestedTestBean", ntb); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -745,7 +746,7 @@ void constructorResourceInjectionWithBeanDefinitionRemoval() { bf.registerSingleton("testBean", tb); bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NestedTestBean.class)); - ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -755,7 +756,7 @@ void constructorResourceInjectionWithBeanDefinitionRemoval() { bf.removeBeanDefinition("nestedTestBean"); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -765,7 +766,7 @@ void constructorResourceInjectionWithBeanDefinitionRemoval() { bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NestedTestBean.class)); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -784,7 +785,7 @@ void constructorResourceInjectionWithNullFromFactoryBean() { bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NullNestedTestBeanFactoryBean.class)); bf.registerSingleton("nestedTestBean2", new NestedTestBean()); - ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -792,7 +793,7 @@ void constructorResourceInjectionWithNullFromFactoryBean() { assertThat(bean.getNestedTestBean()).isNull(); assertThat(bean.getBeanFactory()).isSameAs(bf); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getTestBean2()).isSameAs(tb); assertThat(bean.getTestBean3()).isSameAs(tb); @@ -814,7 +815,7 @@ void constructorResourceInjectionWithNullFromFactoryMethod() { bf.registerBeanDefinition("nestedTestBean", ntb); bf.registerSingleton("nestedTestBean2", new NestedTestBean()); - ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); @@ -822,7 +823,7 @@ void constructorResourceInjectionWithNullFromFactoryMethod() { assertThat(bean.getNestedTestBean()).isNull(); assertThat(bean.getBeanFactory()).isSameAs(bf); - bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", ConstructorResourceInjectionBean.class); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); assertThat(bean.getTestBean3()).isNull(); @@ -841,7 +842,7 @@ void constructorResourceInjectionWithMultipleCandidates() { NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsResourceInjectionBean.class); assertThat(bean.getTestBean3()).isNull(); assertThat(bean.getTestBean4()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); @@ -852,9 +853,9 @@ void constructorResourceInjectionWithMultipleCandidates() { @Test void constructorResourceInjectionWithNoCandidatesAndNoFallback() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class)); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("annotatedBean")) - .satisfies(methodParameterDeclaredOn(ConstructorWithoutFallbackBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("annotatedBean")) + .satisfies(methodParameterDeclaredOn(ConstructorWithoutFallbackBean.class)); } @Test @@ -867,7 +868,7 @@ void constructorResourceInjectionWithCollectionAndNullFromFactoryBean() { NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsCollectionResourceInjectionBean.class); assertThat(bean.getTestBean3()).isNull(); assertThat(bean.getTestBean4()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(1); @@ -889,7 +890,7 @@ void constructorResourceInjectionWithMultipleCandidatesAsCollection() { NestedTestBean ntb2 = new NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsCollectionResourceInjectionBean.class); assertThat(bean.getTestBean3()).isNull(); assertThat(bean.getTestBean4()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); @@ -907,7 +908,7 @@ void constructorResourceInjectionWithMultipleOrderedCandidates() { FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsResourceInjectionBean.class); assertThat(bean.getTestBean3()).isNull(); assertThat(bean.getTestBean4()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); @@ -925,7 +926,7 @@ void constructorResourceInjectionWithMultipleCandidatesAsOrderedCollection() { FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - ConstructorsCollectionResourceInjectionBean bean = (ConstructorsCollectionResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsCollectionResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsCollectionResourceInjectionBean.class); assertThat(bean.getTestBean3()).isNull(); assertThat(bean.getTestBean4()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); @@ -943,7 +944,7 @@ void singleConstructorInjectionWithMultipleCandidatesAsRequiredVararg() { FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - SingleConstructorVarargBean bean = (SingleConstructorVarargBean) bf.getBean("annotatedBean"); + SingleConstructorVarargBean bean = bf.getBean("annotatedBean", SingleConstructorVarargBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); @@ -956,7 +957,7 @@ void singleConstructorInjectionWithEmptyVararg() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - SingleConstructorVarargBean bean = (SingleConstructorVarargBean) bf.getBean("annotatedBean"); + SingleConstructorVarargBean bean = bf.getBean("annotatedBean", SingleConstructorVarargBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).isNotNull(); assertThat(bean.getNestedTestBeans().isEmpty()).isTrue(); @@ -972,7 +973,7 @@ void singleConstructorInjectionWithMultipleCandidatesAsRequiredCollection() { FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - SingleConstructorRequiredCollectionBean bean = (SingleConstructorRequiredCollectionBean) bf.getBean("annotatedBean"); + SingleConstructorRequiredCollectionBean bean = bf.getBean("annotatedBean", SingleConstructorRequiredCollectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); @@ -985,7 +986,7 @@ void singleConstructorInjectionWithEmptyCollection() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - SingleConstructorRequiredCollectionBean bean = (SingleConstructorRequiredCollectionBean) bf.getBean("annotatedBean"); + SingleConstructorRequiredCollectionBean bean = bf.getBean("annotatedBean", SingleConstructorRequiredCollectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).isNotNull(); assertThat(bean.getNestedTestBeans().isEmpty()).isTrue(); @@ -1001,7 +1002,7 @@ void singleConstructorInjectionWithMultipleCandidatesAsOrderedCollection() { FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); bf.registerSingleton("nestedTestBean2", ntb2); - SingleConstructorOptionalCollectionBean bean = (SingleConstructorOptionalCollectionBean) bf.getBean("annotatedBean"); + SingleConstructorOptionalCollectionBean bean = bf.getBean("annotatedBean", SingleConstructorOptionalCollectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).hasSize(2); assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2); @@ -1014,7 +1015,7 @@ void singleConstructorInjectionWithEmptyCollectionAsNull() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - SingleConstructorOptionalCollectionBean bean = (SingleConstructorOptionalCollectionBean) bf.getBean("annotatedBean"); + SingleConstructorOptionalCollectionBean bean = bf.getBean("annotatedBean", SingleConstructorOptionalCollectionBean.class); assertThat(bean.getTestBean()).isSameAs(tb); assertThat(bean.getNestedTestBeans()).isNull(); } @@ -1022,8 +1023,8 @@ void singleConstructorInjectionWithEmptyCollectionAsNull() { @Test void singleConstructorInjectionWithMissingDependency() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("annotatedBean")); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("annotatedBean")); } @Test @@ -1032,8 +1033,8 @@ void singleConstructorInjectionWithNullDependency() { RootBeanDefinition tb = new RootBeanDefinition(NullFactoryMethods.class); tb.setFactoryMethodName("createTestBean"); bf.registerBeanDefinition("testBean", tb); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("annotatedBean")); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("annotatedBean")); } @Test @@ -1042,7 +1043,7 @@ void constructorResourceInjectionWithMultipleCandidatesAndFallback() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsResourceInjectionBean.class); assertThat(bean.getTestBean3()).isSameAs(tb); assertThat(bean.getTestBean4()).isNull(); } @@ -1051,7 +1052,7 @@ void constructorResourceInjectionWithMultipleCandidatesAndFallback() { void constructorResourceInjectionWithMultipleCandidatesAndDefaultFallback() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); - ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); + ConstructorsResourceInjectionBean bean = bf.getBean("annotatedBean", ConstructorsResourceInjectionBean.class); assertThat(bean.getTestBean3()).isNull(); assertThat(bean.getTestBean4()).isNull(); } @@ -1067,12 +1068,12 @@ void constructorInjectionWithMap() { tb2.setFactoryMethodName("createTestBean"); bf.registerBeanDefinition("testBean2", tb2); - MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); + MapConstructorInjectionBean bean = bf.getBean("annotatedBean", MapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).hasSize(1); assertThat(bean.getTestBeanMap().get("testBean1")).isSameAs(tb1); assertThat(bean.getTestBeanMap().get("testBean2")).isNull(); - bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", MapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).hasSize(1); assertThat(bean.getTestBeanMap().get("testBean1")).isSameAs(tb1); assertThat(bean.getTestBeanMap().get("testBean2")).isNull(); @@ -1088,14 +1089,14 @@ void fieldInjectionWithMap() { bf.registerSingleton("testBean1", tb1); bf.registerSingleton("testBean2", tb2); - MapFieldInjectionBean bean = (MapFieldInjectionBean) bf.getBean("annotatedBean"); + MapFieldInjectionBean bean = bf.getBean("annotatedBean", MapFieldInjectionBean.class); assertThat(bean.getTestBeanMap()).hasSize(2); assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); assertThat(bean.getTestBeanMap().values().contains(tb1)).isTrue(); assertThat(bean.getTestBeanMap().values().contains(tb2)).isTrue(); - bean = (MapFieldInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", MapFieldInjectionBean.class); assertThat(bean.getTestBeanMap()).hasSize(2); assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); assertThat(bean.getTestBeanMap().keySet().contains("testBean2")).isTrue(); @@ -1111,13 +1112,13 @@ void methodInjectionWithMap() { TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); - MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); + MapMethodInjectionBean bean = bf.getBean("annotatedBean", MapMethodInjectionBean.class); assertThat(bean.getTestBeanMap()).hasSize(1); assertThat(bean.getTestBeanMap().keySet().contains("testBean")).isTrue(); assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); assertThat(bean.getTestBean()).isSameAs(tb); - bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", MapMethodInjectionBean.class); assertThat(bean.getTestBeanMap()).hasSize(1); assertThat(bean.getTestBeanMap().keySet().contains("testBean")).isTrue(); assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); @@ -1129,9 +1130,9 @@ void methodInjectionWithMapAndMultipleMatches() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).as("should have failed, more than one bean of type").isThrownBy(() -> - bf.getBean("annotatedBean")) - .satisfies(methodParameterDeclaredOn(MapMethodInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class).as("should have failed, more than one bean of type") + .isThrownBy(() -> bf.getBean("annotatedBean")) + .satisfies(methodParameterDeclaredOn(MapMethodInjectionBean.class)); } @Test @@ -1142,8 +1143,8 @@ void methodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() { rbd2.setAutowireCandidate(false); bf.registerBeanDefinition("testBean2", rbd2); - MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); - TestBean tb = (TestBean) bf.getBean("testBean1"); + MapMethodInjectionBean bean = bf.getBean("annotatedBean", MapMethodInjectionBean.class); + TestBean tb = bf.getBean("testBean1", TestBean.class); assertThat(bean.getTestBeanMap()).hasSize(1); assertThat(bean.getTestBeanMap().keySet().contains("testBean1")).isTrue(); assertThat(bean.getTestBeanMap().values().contains(tb)).isTrue(); @@ -1154,7 +1155,7 @@ void methodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() { void methodInjectionWithMapAndNoMatches() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); - MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); + MapMethodInjectionBean bean = bf.getBean("annotatedBean", MapMethodInjectionBean.class); assertThat(bean.getTestBeanMap()).isNull(); assertThat(bean.getTestBean()).isNull(); } @@ -1170,9 +1171,9 @@ void constructorInjectionWithTypedMapAsBean() { bf.registerSingleton("testBeans", tbm); bf.registerSingleton("otherMap", new Properties()); - MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); + MapConstructorInjectionBean bean = bf.getBean("annotatedBean", MapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(tbm); - bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", MapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(tbm); } @@ -1186,9 +1187,9 @@ void constructorInjectionWithPlainMapAsBean() { bf.registerBeanDefinition("myTestBeanMap", tbm); bf.registerSingleton("otherMap", new HashMap<>()); - MapConstructorInjectionBean bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); + MapConstructorInjectionBean bean = bf.getBean("annotatedBean", MapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); - bean = (MapConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", MapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); } @@ -1203,9 +1204,9 @@ void constructorInjectionWithCustomMapAsBean() { bf.registerSingleton("testBean1", new TestBean()); bf.registerSingleton("testBean2", new TestBean()); - CustomMapConstructorInjectionBean bean = (CustomMapConstructorInjectionBean) bf.getBean("annotatedBean"); + CustomMapConstructorInjectionBean bean = bf.getBean("annotatedBean", CustomMapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); - bean = (CustomMapConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", CustomMapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); } @@ -1216,9 +1217,9 @@ void constructorInjectionWithPlainHashMapAsBean() { bf.registerBeanDefinition("annotatedBean", bd); bf.registerBeanDefinition("myTestBeanMap", new RootBeanDefinition(HashMap.class)); - QualifiedMapConstructorInjectionBean bean = (QualifiedMapConstructorInjectionBean) bf.getBean("annotatedBean"); + QualifiedMapConstructorInjectionBean bean = bf.getBean("annotatedBean", QualifiedMapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); - bean = (QualifiedMapConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", QualifiedMapConstructorInjectionBean.class); assertThat(bean.getTestBeanMap()).isSameAs(bf.getBean("myTestBeanMap")); } @@ -1233,9 +1234,9 @@ void constructorInjectionWithTypedSetAsBean() { bf.registerSingleton("testBeans", tbs); bf.registerSingleton("otherSet", new HashSet<>()); - SetConstructorInjectionBean bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); + SetConstructorInjectionBean bean = bf.getBean("annotatedBean", SetConstructorInjectionBean.class); assertThat(bean.getTestBeanSet()).isSameAs(tbs); - bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", SetConstructorInjectionBean.class); assertThat(bean.getTestBeanSet()).isSameAs(tbs); } @@ -1249,9 +1250,9 @@ void constructorInjectionWithPlainSetAsBean() { bf.registerBeanDefinition("myTestBeanSet", tbs); bf.registerSingleton("otherSet", new HashSet<>()); - SetConstructorInjectionBean bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); + SetConstructorInjectionBean bean = bf.getBean("annotatedBean", SetConstructorInjectionBean.class); assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); - bean = (SetConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", SetConstructorInjectionBean.class); assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); } @@ -1264,9 +1265,9 @@ void constructorInjectionWithCustomSetAsBean() { tbs.setUniqueFactoryMethodName("testBeanSet"); bf.registerBeanDefinition("myTestBeanSet", tbs); - CustomSetConstructorInjectionBean bean = (CustomSetConstructorInjectionBean) bf.getBean("annotatedBean"); + CustomSetConstructorInjectionBean bean = bf.getBean("annotatedBean", CustomSetConstructorInjectionBean.class); assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); - bean = (CustomSetConstructorInjectionBean) bf.getBean("annotatedBean"); + bean = bf.getBean("annotatedBean", CustomSetConstructorInjectionBean.class); assertThat(bean.getTestBeanSet()).isSameAs(bf.getBean("myTestBeanSet")); } @@ -1274,7 +1275,7 @@ void constructorInjectionWithCustomSetAsBean() { void selfReference() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionBean.class)); - SelfInjectionBean bean = (SelfInjectionBean) bf.getBean("annotatedBean"); + SelfInjectionBean bean = bf.getBean("annotatedBean", SelfInjectionBean.class); assertThat(bean.reference).isSameAs(bean); assertThat(bean.referenceCollection).isNull(); } @@ -1284,8 +1285,8 @@ void selfReferenceWithOther() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionBean.class)); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(SelfInjectionBean.class)); - SelfInjectionBean bean = (SelfInjectionBean) bf.getBean("annotatedBean"); - SelfInjectionBean bean2 = (SelfInjectionBean) bf.getBean("annotatedBean2"); + SelfInjectionBean bean = bf.getBean("annotatedBean", SelfInjectionBean.class); + SelfInjectionBean bean2 = bf.getBean("annotatedBean2", SelfInjectionBean.class); assertThat(bean.reference).isSameAs(bean2); assertThat(bean.referenceCollection).hasSize(1); assertThat(bean.referenceCollection.get(0)).isSameAs(bean2); @@ -1295,7 +1296,7 @@ void selfReferenceWithOther() { void selfReferenceCollection() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionCollectionBean.class)); - SelfInjectionCollectionBean bean = (SelfInjectionCollectionBean) bf.getBean("annotatedBean"); + SelfInjectionCollectionBean bean = bf.getBean("annotatedBean", SelfInjectionCollectionBean.class); assertThat(bean.reference).isSameAs(bean); assertThat(bean.referenceCollection).isNull(); } @@ -1305,8 +1306,8 @@ void selfReferenceCollectionWithOther() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionCollectionBean.class)); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(SelfInjectionCollectionBean.class)); - SelfInjectionCollectionBean bean = (SelfInjectionCollectionBean) bf.getBean("annotatedBean"); - SelfInjectionCollectionBean bean2 = (SelfInjectionCollectionBean) bf.getBean("annotatedBean2"); + SelfInjectionCollectionBean bean = bf.getBean("annotatedBean", SelfInjectionCollectionBean.class); + SelfInjectionCollectionBean bean2 = bf.getBean("annotatedBean2", SelfInjectionCollectionBean.class); assertThat(bean.reference).isSameAs(bean2); assertThat(bean2.referenceCollection).hasSize(1); assertThat(bean.referenceCollection.get(0)).isSameAs(bean2); @@ -1317,7 +1318,7 @@ void objectFactoryFieldInjection() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); - ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryFieldInjectionBean bean = bf.getBean("annotatedBean", ObjectFactoryFieldInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @@ -1326,7 +1327,7 @@ void objectFactoryConstructorInjection() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryConstructorInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); - ObjectFactoryConstructorInjectionBean bean = (ObjectFactoryConstructorInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryConstructorInjectionBean bean = bf.getBean("annotatedBean", ObjectFactoryConstructorInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); } @@ -1337,9 +1338,9 @@ void objectFactoryInjectionIntoPrototypeBean() { bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); - ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryFieldInjectionBean bean = bf.getBean("annotatedBean", ObjectFactoryFieldInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); - ObjectFactoryFieldInjectionBean anotherBean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryFieldInjectionBean anotherBean = bf.getBean("annotatedBean", ObjectFactoryFieldInjectionBean.class); assertThat(bean).isNotSameAs(anotherBean); assertThat(anotherBean.getTestBean()).isSameAs(bf.getBean("testBean")); } @@ -1352,7 +1353,7 @@ void objectFactoryQualifierInjection() { bf.registerBeanDefinition("dependencyBean", bd); bf.registerBeanDefinition("dependencyBean2", new RootBeanDefinition(TestBean.class)); - ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryQualifierInjectionBean bean = bf.getBean("annotatedBean", ObjectFactoryQualifierInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("dependencyBean")); } @@ -1364,7 +1365,7 @@ void objectFactoryQualifierProviderInjection() { bf.registerBeanDefinition("dependencyBean", bd); bf.registerBeanDefinition("dependencyBean2", new RootBeanDefinition(TestBean.class)); - ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryQualifierInjectionBean bean = bf.getBean("annotatedBean", ObjectFactoryQualifierInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("dependencyBean")); } @@ -1374,7 +1375,7 @@ void objectFactorySerialization() throws Exception { bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); - ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); + ObjectFactoryFieldInjectionBean bean = bf.getBean("annotatedBean", ObjectFactoryFieldInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); bean = SerializationTestUtils.serializeAndDeserialize(bean); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); @@ -1387,7 +1388,7 @@ void objectProviderInjectionWithPrototype() { tbd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("testBean", tbd); - ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); + ObjectProviderInjectionBean bean = bf.getBean("annotatedBean", ObjectProviderInjectionBean.class); assertThat(bean.getTestBean()).isEqualTo(bf.getBean("testBean")); assertThat(bean.getTestBean("myName")).isEqualTo(bf.getBean("testBean", "myName")); assertThat(bean.getOptionalTestBean()).isEqualTo(bf.getBean("testBean")); @@ -1416,7 +1417,7 @@ void objectProviderInjectionWithSingletonTarget() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); - ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); + ObjectProviderInjectionBean bean = bf.getBean("annotatedBean", ObjectProviderInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean")); assertThat(bean.getOptionalTestBean()).isSameAs(bf.getBean("testBean")); assertThat(bean.getOptionalTestBeanWithDefault()).isSameAs(bf.getBean("testBean")); @@ -1443,9 +1444,8 @@ void objectProviderInjectionWithSingletonTarget() { void objectProviderInjectionWithTargetNotAvailable() { bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); - ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); - assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy( - bean::getTestBean); + ObjectProviderInjectionBean bean = bf.getBean("annotatedBean", ObjectProviderInjectionBean.class); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(bean::getTestBean); assertThat(bean.getOptionalTestBean()).isNull(); assertThat(bean.consumeOptionalTestBean()).isNull(); assertThat(bean.getOptionalTestBeanWithDefault()).isEqualTo(new TestBean("default")); @@ -1469,7 +1469,7 @@ void objectProviderInjectionWithTargetNotUnique() { bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); - ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); + ObjectProviderInjectionBean bean = bf.getBean("annotatedBean", ObjectProviderInjectionBean.class); assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(bean::getTestBean); assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(bean::getOptionalTestBean); assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(bean::consumeOptionalTestBean); @@ -1506,7 +1506,7 @@ void objectProviderInjectionWithTargetPrimary() { tb2.setLazyInit(true); bf.registerBeanDefinition("testBean2", tb2); - ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); + ObjectProviderInjectionBean bean = bf.getBean("annotatedBean", ObjectProviderInjectionBean.class); assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean1")); assertThat(bean.getOptionalTestBean()).isSameAs(bf.getBean("testBean1")); assertThat(bean.consumeOptionalTestBean()).isSameAs(bf.getBean("testBean1")); @@ -1544,7 +1544,7 @@ void objectProviderInjectionWithUnresolvedOrderedStream() { tb2.setLazyInit(true); bf.registerBeanDefinition("testBean2", tb2); - ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); + ObjectProviderInjectionBean bean = bf.getBean("annotatedBean", ObjectProviderInjectionBean.class); List testBeans = bean.sortedTestBeans(); assertThat(testBeans).hasSize(2); assertThat(testBeans.get(0)).isSameAs(bf.getBean("testBean2")); @@ -1573,9 +1573,9 @@ void customAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound() bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredFieldResourceInjectionBean.class)); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("customBean")) - .satisfies(fieldDeclaredOn(CustomAnnotationRequiredFieldResourceInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("customBean")) + .satisfies(fieldDeclaredOn(CustomAnnotationRequiredFieldResourceInjectionBean.class)); } @Test @@ -1589,9 +1589,9 @@ void customAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependencies bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("customBean")) - .satisfies(fieldDeclaredOn(CustomAnnotationRequiredFieldResourceInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("customBean")) + .satisfies(fieldDeclaredOn(CustomAnnotationRequiredFieldResourceInjectionBean.class)); } @Test @@ -1616,9 +1616,9 @@ void customAnnotationRequiredMethodResourceInjectionFailsWhenNoDependencyFound() bpp.setRequiredParameterValue(false); bf.registerBeanDefinition("customBean", new RootBeanDefinition( CustomAnnotationRequiredMethodResourceInjectionBean.class)); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("customBean")) - .satisfies(methodParameterDeclaredOn(CustomAnnotationRequiredMethodResourceInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("customBean")) + .satisfies(methodParameterDeclaredOn(CustomAnnotationRequiredMethodResourceInjectionBean.class)); } @Test @@ -1632,9 +1632,9 @@ void customAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependencie bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("customBean")) - .satisfies(methodParameterDeclaredOn(CustomAnnotationRequiredMethodResourceInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("customBean")) + .satisfies(methodParameterDeclaredOn(CustomAnnotationRequiredMethodResourceInjectionBean.class)); } @Test @@ -1680,9 +1680,9 @@ void customAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("customBean")) - .satisfies(fieldDeclaredOn(CustomAnnotationOptionalFieldResourceInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("customBean")) + .satisfies(fieldDeclaredOn(CustomAnnotationOptionalFieldResourceInjectionBean.class)); } @Test @@ -1696,7 +1696,7 @@ void customAnnotationOptionalMethodResourceInjection() { bf.registerSingleton("testBean", tb); CustomAnnotationOptionalMethodResourceInjectionBean bean = - (CustomAnnotationOptionalMethodResourceInjectionBean) bf.getBean("customBean"); + bf.getBean("customBean", CustomAnnotationOptionalMethodResourceInjectionBean.class); assertThat(bean.getTestBean3()).isSameAs(tb); assertThat(bean.getTestBean()).isNull(); assertThat(bean.getTestBean2()).isNull(); @@ -1728,9 +1728,9 @@ void customAnnotationOptionalMethodResourceInjectionWhenMultipleDependenciesFoun bf.registerSingleton("testBean1", tb1); TestBean tb2 = new TestBean(); bf.registerSingleton("testBean2", tb2); - assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> - bf.getBean("customBean")) - .satisfies(methodParameterDeclaredOn(CustomAnnotationOptionalMethodResourceInjectionBean.class)); + assertThatExceptionOfType(UnsatisfiedDependencyException.class) + .isThrownBy(() -> bf.getBean("customBean")) + .satisfies(methodParameterDeclaredOn(CustomAnnotationOptionalMethodResourceInjectionBean.class)); } /** @@ -1767,7 +1767,7 @@ void genericsBasedFieldInjection() { IntegerRepository ir = new IntegerRepository(); bf.registerSingleton("integerRepo", ir); - RepositoryFieldInjectionBean bean = (RepositoryFieldInjectionBean) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBean bean = bf.getBean("annotatedBean", RepositoryFieldInjectionBean.class); assertThat(bean.string).isSameAs(sv); assertThat(bean.integer).isSameAs(iv); assertThat(bean.stringArray).hasSize(1); @@ -1812,7 +1812,8 @@ void genericsBasedFieldInjectionWithSubstitutedVariables() { IntegerRepository ir = new IntegerRepository(); bf.registerSingleton("integerRepo", ir); - RepositoryFieldInjectionBeanWithSubstitutedVariables bean = (RepositoryFieldInjectionBeanWithSubstitutedVariables) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBeanWithSubstitutedVariables bean = + bf.getBean("annotatedBean", RepositoryFieldInjectionBeanWithSubstitutedVariables.class); assertThat(bean.string).isSameAs(sv); assertThat(bean.integer).isSameAs(iv); assertThat(bean.stringArray).hasSize(1); @@ -1853,7 +1854,8 @@ void genericsBasedFieldInjectionWithQualifiers() { IntegerRepository ir = new IntegerRepository(); bf.registerSingleton("integerRepo", ir); - RepositoryFieldInjectionBeanWithQualifiers bean = (RepositoryFieldInjectionBeanWithQualifiers) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBeanWithQualifiers bean = + bf.getBean("annotatedBean", RepositoryFieldInjectionBeanWithQualifiers.class); assertThat(bean.stringRepository).isSameAs(sr); assertThat(bean.integerRepository).isSameAs(ir); assertThat(bean.stringRepositoryArray).hasSize(1); @@ -1890,7 +1892,8 @@ void genericsBasedFieldInjectionWithMocks() { rbd.setQualifiedElement(ReflectionUtils.findField(getClass(), "integerRepositoryQualifierProvider")); bf.registerBeanDefinition("integerRepository", rbd); // Bean name not matching qualifier - RepositoryFieldInjectionBeanWithQualifiers bean = (RepositoryFieldInjectionBeanWithQualifiers) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBeanWithQualifiers bean = + bf.getBean("annotatedBean", RepositoryFieldInjectionBeanWithQualifiers.class); Repository sr = bf.getBean("stringRepo", Repository.class); Repository ir = bf.getBean("integerRepository", Repository.class); assertThat(bean.stringRepository).isSameAs(sr); @@ -1917,7 +1920,8 @@ void genericsBasedFieldInjectionWithSimpleMatch() { bf.registerSingleton("repo", new StringRepository()); - RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBeanWithSimpleMatch bean = + bf.getBean("annotatedBean", RepositoryFieldInjectionBeanWithSimpleMatch.class); Repository repo = bf.getBean("repo", Repository.class); assertThat(bean.repository).isSameAs(repo); assertThat(bean.stringRepository).isSameAs(repo); @@ -1944,7 +1948,7 @@ void genericsBasedFactoryBeanInjectionWithBeanDefinition() { bf.registerBeanDefinition("annotatedBean", bd); bf.registerBeanDefinition("repoFactoryBean", new RootBeanDefinition(RepositoryFactoryBean.class)); - RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) bf.getBean("annotatedBean"); + RepositoryFactoryBeanInjectionBean bean = bf.getBean("annotatedBean", RepositoryFactoryBeanInjectionBean.class); RepositoryFactoryBean repoFactoryBean = bf.getBean("&repoFactoryBean", RepositoryFactoryBean.class); assertThat(bean.repositoryFactoryBean).isSameAs(repoFactoryBean); } @@ -1956,7 +1960,7 @@ void genericsBasedFactoryBeanInjectionWithSingletonBean() { bf.registerBeanDefinition("annotatedBean", bd); bf.registerSingleton("repoFactoryBean", new RepositoryFactoryBean<>()); - RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) bf.getBean("annotatedBean"); + RepositoryFactoryBeanInjectionBean bean = bf.getBean("annotatedBean", RepositoryFactoryBeanInjectionBean.class); RepositoryFactoryBean repoFactoryBean = bf.getBean("&repoFactoryBean", RepositoryFactoryBean.class); assertThat(bean.repositoryFactoryBean).isSameAs(repoFactoryBean); } @@ -1975,7 +1979,7 @@ void genericsBasedFieldInjectionWithSimpleMatchAndMock() { rbd.getConstructorArgumentValues().addGenericArgumentValue(Repository.class); bf.registerBeanDefinition("repo", rbd); - RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBeanWithSimpleMatch bean = bf.getBean("annotatedBean", RepositoryFieldInjectionBeanWithSimpleMatch.class); Repository repo = bf.getBean("repo", Repository.class); assertThat(bean.repository).isSameAs(repo); assertThat(bean.stringRepository).isSameAs(repo); @@ -2006,7 +2010,7 @@ void genericsBasedFieldInjectionWithSimpleMatchAndMockito() { rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Repository.class.getName())); bf.registerBeanDefinition("repo", rbd); - RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean"); + RepositoryFieldInjectionBeanWithSimpleMatch bean = bf.getBean("annotatedBean", RepositoryFieldInjectionBeanWithSimpleMatch.class); Repository repo = bf.getBean("repo", Repository.class); assertThat(bean.repository).isSameAs(repo); assertThat(bean.stringRepository).isSameAs(repo); @@ -2047,7 +2051,7 @@ void genericsBasedMethodInjection() { IntegerRepository ir = new IntegerRepository(); bf.registerSingleton("integerRepo", ir); - RepositoryMethodInjectionBean bean = (RepositoryMethodInjectionBean) bf.getBean("annotatedBean"); + RepositoryMethodInjectionBean bean = bf.getBean("annotatedBean", RepositoryMethodInjectionBean.class); assertThat(bean.string).isSameAs(sv); assertThat(bean.integer).isSameAs(iv); assertThat(bean.stringArray).hasSize(1); @@ -2092,7 +2096,8 @@ void genericsBasedMethodInjectionWithSubstitutedVariables() { IntegerRepository ir = new IntegerRepository(); bf.registerSingleton("integerRepo", ir); - RepositoryMethodInjectionBeanWithSubstitutedVariables bean = (RepositoryMethodInjectionBeanWithSubstitutedVariables) bf.getBean("annotatedBean"); + RepositoryMethodInjectionBeanWithSubstitutedVariables bean = + bf.getBean("annotatedBean", RepositoryMethodInjectionBeanWithSubstitutedVariables.class); assertThat(bean.string).isSameAs(sv); assertThat(bean.integer).isSameAs(iv); assertThat(bean.stringArray).hasSize(1); @@ -2133,7 +2138,7 @@ void genericsBasedConstructorInjection() { IntegerRepository ir = new IntegerRepository(); bf.registerSingleton("integerRepo", ir); - RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); + RepositoryConstructorInjectionBean bean = bf.getBean("annotatedBean", RepositoryConstructorInjectionBean.class); assertThat(bean.stringRepository).isSameAs(sr); assertThat(bean.integerRepository).isSameAs(ir); assertThat(bean.stringRepositoryArray).hasSize(1); @@ -2159,7 +2164,7 @@ void genericsBasedConstructorInjectionWithNonTypedTarget() { GenericRepository gr = new GenericRepository(); bf.registerSingleton("genericRepo", gr); - RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); + RepositoryConstructorInjectionBean bean = bf.getBean("annotatedBean", RepositoryConstructorInjectionBean.class); assertThat(bean.stringRepository).isSameAs(gr); assertThat(bean.integerRepository).isSameAs(gr); assertThat(bean.stringRepositoryArray).hasSize(1); @@ -2184,7 +2189,7 @@ void genericsBasedConstructorInjectionWithNonGenericTarget() { SimpleRepository ngr = new SimpleRepository(); bf.registerSingleton("simpleRepo", ngr); - RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); + RepositoryConstructorInjectionBean bean = bf.getBean("annotatedBean", RepositoryConstructorInjectionBean.class); assertThat(bean.stringRepository).isSameAs(ngr); assertThat(bean.integerRepository).isSameAs(ngr); assertThat(bean.stringRepositoryArray).hasSize(1); @@ -2212,7 +2217,7 @@ void genericsBasedConstructorInjectionWithMixedTargets() { GenericRepository gr = new GenericRepositorySubclass(); bf.registerSingleton("genericRepo", gr); - RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); + RepositoryConstructorInjectionBean bean = bf.getBean("annotatedBean", RepositoryConstructorInjectionBean.class); assertThat(bean.stringRepository).isSameAs(sr); assertThat(bean.integerRepository).isSameAs(gr); assertThat(bean.stringRepositoryArray).hasSize(1); @@ -2239,7 +2244,7 @@ void genericsBasedConstructorInjectionWithMixedTargetsIncludingNonGeneric() { SimpleRepository ngr = new SimpleRepositorySubclass(); bf.registerSingleton("simpleRepo", ngr); - RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean"); + RepositoryConstructorInjectionBean bean = bf.getBean("annotatedBean", RepositoryConstructorInjectionBean.class); assertThat(bean.stringRepository).isSameAs(sr); assertThat(bean.integerRepository).isSameAs(ngr); assertThat(bean.stringRepositoryArray).hasSize(1);