|
29 | 29 | import org.springframework.aot.generate.GenerationContext;
|
30 | 30 | import org.springframework.aot.generate.MethodReference;
|
31 | 31 | import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
|
| 32 | +import org.springframework.aot.generate.ValueCodeGenerationException; |
32 | 33 | import org.springframework.aot.hint.MemberCategory;
|
33 | 34 | import org.springframework.aot.test.generate.TestGenerationContext;
|
34 | 35 | import org.springframework.beans.factory.aot.BeanRegistrationsAotContribution.Registration;
|
|
38 | 39 | import org.springframework.beans.testfixture.beans.AgeHolder;
|
39 | 40 | import org.springframework.beans.testfixture.beans.Employee;
|
40 | 41 | import org.springframework.beans.testfixture.beans.ITestBean;
|
| 42 | +import org.springframework.beans.testfixture.beans.NestedTestBean; |
41 | 43 | import org.springframework.beans.testfixture.beans.TestBean;
|
42 | 44 | import org.springframework.beans.testfixture.beans.factory.aot.MockBeanFactoryInitializationCode;
|
43 | 45 | import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
|
|
50 | 52 | import org.springframework.javapoet.ParameterizedTypeName;
|
51 | 53 |
|
52 | 54 | import static org.assertj.core.api.Assertions.assertThat;
|
| 55 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
53 | 56 | import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
|
54 | 57 |
|
55 | 58 | /**
|
@@ -156,6 +159,57 @@ void applyToRegisterReflectionHints() {
|
156 | 159 | .accepts(this.generationContext.getRuntimeHints());
|
157 | 160 | }
|
158 | 161 |
|
| 162 | + @Test |
| 163 | + void applyToFailingDoesNotWrapAotException() { |
| 164 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(TestBean.class); |
| 165 | + beanDefinition.setInstanceSupplier(TestBean::new); |
| 166 | + RegisteredBean registeredBean = registerBean(beanDefinition); |
| 167 | + |
| 168 | + BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, |
| 169 | + registeredBean, null, List.of()); |
| 170 | + BeanRegistrationsAotContribution contribution = createContribution(registeredBean, generator, "testAlias"); |
| 171 | + assertThatExceptionOfType(AotProcessingException.class) |
| 172 | + .isThrownBy(() -> contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode)) |
| 173 | + .withMessage("Error processing bean with name 'testBean': instance supplier is not supported") |
| 174 | + .withNoCause(); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void applyToFailingWrapsValueCodeGeneration() { |
| 179 | + RootBeanDefinition beanDefinition = new RootBeanDefinition(TestBean.class); |
| 180 | + beanDefinition.getPropertyValues().addPropertyValue("doctor", new NestedTestBean()); |
| 181 | + RegisteredBean registeredBean = registerBean(beanDefinition); |
| 182 | + |
| 183 | + BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, |
| 184 | + registeredBean, null, List.of()); |
| 185 | + BeanRegistrationsAotContribution contribution = createContribution(registeredBean, generator, "testAlias"); |
| 186 | + assertThatExceptionOfType(AotProcessingException.class) |
| 187 | + .isThrownBy(() -> contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode)) |
| 188 | + .withMessage("Error processing bean with name 'testBean': failed to generate code for bean definition") |
| 189 | + .havingCause().isInstanceOf(ValueCodeGenerationException.class) |
| 190 | + .withMessageContaining("Failed to generate code for") |
| 191 | + .withMessageContaining(NestedTestBean.class.getName()); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + void applyToFailingProvidesDedicatedException() { |
| 196 | + RegisteredBean registeredBean = registerBean(new RootBeanDefinition(TestBean.class)); |
| 197 | + |
| 198 | + BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, |
| 199 | + registeredBean, null, List.of()) { |
| 200 | + @Override |
| 201 | + MethodReference generateBeanDefinitionMethod(GenerationContext generationContext, |
| 202 | + BeanRegistrationsCode beanRegistrationsCode) { |
| 203 | + throw new IllegalStateException("Test exception"); |
| 204 | + } |
| 205 | + }; |
| 206 | + BeanRegistrationsAotContribution contribution = createContribution(registeredBean, generator, "testAlias"); |
| 207 | + assertThatExceptionOfType(AotProcessingException.class) |
| 208 | + .isThrownBy(() -> contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode)) |
| 209 | + .withMessage("Error processing bean with name 'testBean': failed to generate code for bean definition") |
| 210 | + .havingCause().isInstanceOf(IllegalStateException.class).withMessage("Test exception"); |
| 211 | + } |
| 212 | + |
159 | 213 | private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {
|
160 | 214 | String beanName = "testBean";
|
161 | 215 | this.beanFactory.registerBeanDefinition(beanName, rootBeanDefinition);
|
|
0 commit comments