Skip to content

Commit dc6b2ab

Browse files
committed
Verify scope support for 'lite' @beans in the TCF
Introduced AtBeanLiteModeScopeTests integration tests to verify proper scoping of beans created in 'lite' mode. Updated comments in TACCWithoutACTests to better reflect the runtime behavior for 'lite' @bean methods. Issue: SPR-9401
1 parent 4996625 commit dc6b2ab

File tree

2 files changed

+111
-5
lines changed

2 files changed

+111
-5
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.assertFalse;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNotSame;
22+
import static org.junit.Assert.assertSame;
23+
import static org.junit.Assert.assertTrue;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.beans.factory.annotation.Qualifier;
29+
import org.springframework.context.ApplicationContext;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Scope;
32+
import org.springframework.test.context.ContextConfiguration;
33+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34+
35+
/**
36+
* Integration tests that verify proper scoping of beans created in
37+
* <em>{@code @Bean} Lite Mode</em>.
38+
*
39+
* @author Sam Brannen
40+
* @since 3.2
41+
*/
42+
@RunWith(SpringJUnit4ClassRunner.class)
43+
@ContextConfiguration(classes = AtBeanLiteModeScopeTests.LiteBeans.class)
44+
public class AtBeanLiteModeScopeTests {
45+
46+
/**
47+
* This is intentionally <b>not</b> annotated with {@code @Configuration}.
48+
*/
49+
static class LiteBeans {
50+
51+
@Bean
52+
public LifecycleBean singleton() {
53+
LifecycleBean bean = new LifecycleBean("singleton");
54+
assertFalse(bean.isInitialized());
55+
return bean;
56+
}
57+
58+
@Bean
59+
@Scope("prototype")
60+
public LifecycleBean prototype() {
61+
LifecycleBean bean = new LifecycleBean("prototype");
62+
assertFalse(bean.isInitialized());
63+
return bean;
64+
}
65+
}
66+
67+
68+
@Autowired
69+
private ApplicationContext applicationContext;
70+
71+
@Autowired
72+
@Qualifier("singleton")
73+
private LifecycleBean injectedSingletonBean;
74+
75+
@Autowired
76+
@Qualifier("prototype")
77+
private LifecycleBean injectedPrototypeBean;
78+
79+
80+
@Test
81+
public void singletonLiteBean() {
82+
assertNotNull(injectedSingletonBean);
83+
assertTrue(injectedSingletonBean.isInitialized());
84+
85+
LifecycleBean retrievedSingletonBean = applicationContext.getBean("singleton", LifecycleBean.class);
86+
assertNotNull(retrievedSingletonBean);
87+
assertTrue(retrievedSingletonBean.isInitialized());
88+
89+
assertSame(injectedSingletonBean, retrievedSingletonBean);
90+
}
91+
92+
@Test
93+
public void prototypeLiteBean() {
94+
assertNotNull(injectedPrototypeBean);
95+
assertTrue(injectedPrototypeBean.isInitialized());
96+
97+
LifecycleBean retrievedPrototypeBean = applicationContext.getBean("prototype", LifecycleBean.class);
98+
assertNotNull(retrievedPrototypeBean);
99+
assertTrue(retrievedPrototypeBean.isInitialized());
100+
101+
assertNotSame(injectedPrototypeBean, retrievedPrototypeBean);
102+
}
103+
104+
}

spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,23 @@ public PlatformTransactionManager transactionManager() {
7373

7474
/**
7575
* Since this method does not reside in a true {@code @Configuration class},
76-
* it acts as a factory method instead of a singleton bean. The result is
77-
* that this method will be called at least twice:
76+
* it acts as a factory method when invoked directly (e.g., from
77+
* {@link #transactionManager()}) and as a singleton bean when retrieved
78+
* through the application context (e.g., when injected into the test
79+
* instance). The result is that this method will be called twice:
7880
*
79-
* <ul>
81+
* <ol>
8082
* <li>once <em>indirectly</em> by the {@link TransactionalTestExecutionListener}
8183
* when it retrieves the {@link PlatformTransactionManager} from the
8284
* application context</li>
8385
* <li>and again when the {@link DataSource} is injected into the test
8486
* instance in {@link AbstractTransactionalAnnotatedConfigClassTests#setDataSource(DataSource)}.</li>
85-
*</ul>
87+
*</ol>
8688
*
8789
* Consequently, the {@link JdbcTemplate} used by this test instance and
8890
* the {@link PlatformTransactionManager} used by the Spring TestContext
8991
* Framework will operate on two different {@code DataSource} instances,
90-
* which is most certainly not the desired or intended behavior.
92+
* which is almost certainly not the desired or intended behavior.
9193
*/
9294
@Bean
9395
public DataSource dataSource() {

0 commit comments

Comments
 (0)