|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.bean.override.convention; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import org.springframework.test.context.bean.override.example.ExampleService; |
| 24 | +import org.springframework.test.context.bean.override.example.RealExampleService; |
| 25 | +import org.springframework.test.context.junit.EngineTestKitUtils; |
| 26 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.fail; |
| 29 | +import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; |
| 30 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.cause; |
| 31 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; |
| 32 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; |
| 33 | + |
| 34 | +/** |
| 35 | + * {@link TestBean @TestBean} "by type" integration tests for failure scenarios. |
| 36 | + * |
| 37 | + * @author Simon Baslé |
| 38 | + * @author Sam Brannen |
| 39 | + * @since 6.2 |
| 40 | + * @see TestBeanByTypeIntegrationTests |
| 41 | + */ |
| 42 | +class FailingTestBeanByTypeIntegrationTests { |
| 43 | + |
| 44 | + @Test |
| 45 | + void zeroCandidates() { |
| 46 | + Class<?> testClass = NoMatchingBeansTestCase.class; |
| 47 | + EngineTestKitUtils.executeTestsForClass(testClass).assertThatEvents().haveExactly(1, |
| 48 | + finishedWithFailure( |
| 49 | + cause( |
| 50 | + instanceOf(IllegalStateException.class), |
| 51 | + message(""" |
| 52 | + Unable to select a bean definition to override: 0 bean definitions \ |
| 53 | + found of type %s (as required by annotated field '%s.example')""" |
| 54 | + .formatted(ExampleService.class.getName(), testClass.getSimpleName()))))); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void tooManyCandidates() { |
| 59 | + Class<?> testClass = TooManyBeansTestCase.class; |
| 60 | + EngineTestKitUtils.executeTestsForClass(testClass).assertThatEvents().haveExactly(1, |
| 61 | + finishedWithFailure( |
| 62 | + cause( |
| 63 | + instanceOf(IllegalStateException.class), |
| 64 | + message(""" |
| 65 | + Unable to select a bean definition to override: 2 bean definitions \ |
| 66 | + found of type %s (as required by annotated field '%s.example')""" |
| 67 | + .formatted(ExampleService.class.getName(), testClass.getSimpleName()))))); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @SpringJUnitConfig |
| 72 | + static class NoMatchingBeansTestCase { |
| 73 | + |
| 74 | + @TestBean |
| 75 | + ExampleService example; |
| 76 | + |
| 77 | + @Test |
| 78 | + void test() { |
| 79 | + } |
| 80 | + |
| 81 | + static ExampleService exampleTestOverride() { |
| 82 | + return fail("unexpected override"); |
| 83 | + } |
| 84 | + |
| 85 | + @Configuration |
| 86 | + static class Config { |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + @SpringJUnitConfig |
| 92 | + static class TooManyBeansTestCase { |
| 93 | + |
| 94 | + @TestBean |
| 95 | + ExampleService example; |
| 96 | + |
| 97 | + @Test |
| 98 | + void test() { |
| 99 | + } |
| 100 | + |
| 101 | + static ExampleService exampleTestOverride() { |
| 102 | + return fail("unexpected override"); |
| 103 | + } |
| 104 | + |
| 105 | + @Configuration |
| 106 | + static class Config { |
| 107 | + |
| 108 | + @Bean |
| 109 | + ExampleService bean1() { |
| 110 | + return new RealExampleService("1 Hello"); |
| 111 | + } |
| 112 | + |
| 113 | + @Bean |
| 114 | + ExampleService bean2() { |
| 115 | + return new RealExampleService("2 Hello"); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments