From f2fe7f335c3b55cb2a2f9bd4cada56dc2ad35633 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 14 May 2022 18:30:39 +0200 Subject: [PATCH] Improve nullReturningBeanPostProcessor() test See gh-28459 --- ...notationConfigApplicationContextTests.java | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java b/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java index 566a445288c..c1799f23298 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -148,25 +148,53 @@ class AnnotationConfigApplicationContextTests { void nullReturningBeanPostProcessor() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(AutowiredConfig.class); + // 1st BPP always gets invoked + context.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() { + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) { + if (bean instanceof TestBean) { + TestBean testBean = (TestBean) bean; + testBean.name = testBean.name + "-before"; + } + return bean; + } + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) { + if (bean instanceof TestBean) { + TestBean testBean = (TestBean) bean; + testBean.name = testBean.name + "-after"; + } + return bean; + } + }); + // 2nd BPP always returns null for a TestBean context.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) { return (bean instanceof TestBean ? null : bean); } + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) { + return (bean instanceof TestBean ? null : bean); + } }); + // 3rd BPP never gets invoked with a TestBean context.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) { - bean.getClass().getName(); + assertThat(bean).isNotInstanceOf(TestBean.class); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) { - bean.getClass().getName(); + assertThat(bean).isNotInstanceOf(TestBean.class); return bean; } }); context.refresh(); + TestBean testBean = context.getBean(TestBean.class); + assertThat(testBean).isNotNull(); + assertThat(testBean.name).isEqualTo("foo-before-after"); } @Test