|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 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 | + * http://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.junit4.spr9051; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertFalse; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.test.context.ContextConfiguration; |
| 31 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 32 | + |
| 33 | +/** |
| 34 | + * This set of tests refutes the claims made in |
| 35 | + * <a href="https://jira.springsource.org/browse/SPR-9051" target="_blank">SPR-9051</a>. |
| 36 | + * |
| 37 | + * <p><b>The Claims</b>: |
| 38 | + * |
| 39 | + * <blockquote> |
| 40 | + * When a {@code @ContextConfiguration} test class references a config class |
| 41 | + * missing an {@code @Configuration} annotation, {@code @Bean} dependencies are |
| 42 | + * wired successfully but the bean lifecycle is not applied (no init methods are |
| 43 | + * invoked, for example). Adding the missing {@code @Configuration} annotation |
| 44 | + * solves the problem, however the problem and solution isn't obvious since |
| 45 | + * wiring/injection appeared to work. |
| 46 | + * </blockquote> |
| 47 | + * |
| 48 | + * @author Sam Brannen |
| 49 | + * @since 3.2 |
| 50 | + */ |
| 51 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 52 | +@ContextConfiguration(classes = AnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class) |
| 53 | +public class AnnotatedConfigClassesWithoutAtConfigurationTests { |
| 54 | + |
| 55 | + /** |
| 56 | + * This is intentionally <b>not</b> annotated with {@code @Configuration}. |
| 57 | + * Consequently, this class contains what we call <i>annotated factory bean |
| 58 | + * methods</i> instead of standard bean definition methods. |
| 59 | + */ |
| 60 | + static class AnnotatedFactoryBeans { |
| 61 | + |
| 62 | + static final AtomicInteger enigmaCallCount = new AtomicInteger(); |
| 63 | + |
| 64 | + |
| 65 | + @Bean |
| 66 | + public String enigma() { |
| 67 | + return "enigma #" + enigmaCallCount.incrementAndGet(); |
| 68 | + } |
| 69 | + |
| 70 | + @Bean |
| 71 | + public LifecycleBean lifecycleBean() { |
| 72 | + // The following call to enigma() literally invokes the local |
| 73 | + // enigma() method, not a CGLIB proxied version, since these methods |
| 74 | + // are essentially factory bean methods. |
| 75 | + LifecycleBean bean = new LifecycleBean(enigma()); |
| 76 | + assertFalse(bean.isInitialized()); |
| 77 | + return bean; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Autowired |
| 83 | + private String enigma; |
| 84 | + |
| 85 | + @Autowired |
| 86 | + private LifecycleBean lifecycleBean; |
| 87 | + |
| 88 | + |
| 89 | + @Test |
| 90 | + public void simpleStringBean() { |
| 91 | + assertNotNull(enigma); |
| 92 | + assertEquals("enigma #1", enigma); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void beanWithLifecycleCallback() { |
| 97 | + assertNotNull(lifecycleBean); |
| 98 | + assertEquals("enigma #2", lifecycleBean.getName()); |
| 99 | + assertTrue(lifecycleBean.isInitialized()); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments