From e71cd06a465228d54d373431f4b51a417013bea0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 19 May 2012 04:03:40 +0200 Subject: [PATCH] Doc. usage of JSR-250 lifecycle annotations in TCF The reference manual previously did not mention the applicability of JSR-250 lifecycle annotations within the TestContext framework. The lacking documentation here has lead to misunderstandings of the support provided for @PostConstruct and @PreDestroy in test classes. The testing chapter of the reference manual has therefore been updated to explicitly define the limited support for these annotations. Also introduced Jsr250LifecycleTests for empirical verification of the expected behavior. Issue: SPR-4868 --- .../junit4/spr4868/Jsr250LifecycleTests.java | 118 ++++++++++++++++++ .../context/junit4/spr4868/LifecycleBean.java | 44 +++++++ src/reference/docbook/testing.xml | 26 ++++ 3 files changed, 188 insertions(+) create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/Jsr250LifecycleTests.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/LifecycleBean.java diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/Jsr250LifecycleTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/Jsr250LifecycleTests.java new file mode 100644 index 0000000000..45af06fb21 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/Jsr250LifecycleTests.java @@ -0,0 +1,118 @@ +/* + * Copyright 2002-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.spr4868; + +import static org.junit.Assert.assertNotNull; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; + +/** + * Integration tests that investigate the applicability of JSR-250 lifecycle + * annotations in test classes. + * + *

This class does not really contain actual tests per se. Rather it + * can be used to empirically verify the expected log output (see below). In + * order to see the log output, one would naturally need to ensure that the + * logger category for this class is enabled at {@code INFO} level. + * + *

Expected Log Output

+ *
+ * INFO : org.springframework.test.context.junit4.spr4868.LifecycleBean - initializing
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - beforeAllTests()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - setUp()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - test1()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - tearDown()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - beforeAllTests()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - setUp()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - test2()
+ * INFO : org.springframework.test.context.junit4.spr4868.ExampleTest - tearDown()
+ * INFO : org.springframework.test.context.junit4.spr4868.LifecycleBean - destroying
+ * 
+ * + * @author Sam Brannen + * @since 3.2 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class }) +@ContextConfiguration +public class Jsr250LifecycleTests { + + private final Log logger = LogFactory.getLog(Jsr250LifecycleTests.class); + + + @Configuration + static class Config { + + @Bean + public LifecycleBean lifecycleBean() { + return new LifecycleBean(); + } + } + + + @Autowired + private LifecycleBean lifecycleBean; + + + @PostConstruct + public void beforeAllTests() { + logger.info("beforeAllTests()"); + } + + @PreDestroy + public void afterTestSuite() { + logger.info("afterTestSuite()"); + } + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + } + + @Test + public void test1() { + logger.info("test1()"); + assertNotNull(lifecycleBean); + } + + @Test + public void test2() { + logger.info("test2()"); + assertNotNull(lifecycleBean); + } + +} \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/LifecycleBean.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/LifecycleBean.java new file mode 100644 index 0000000000..1b8dda709d --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr4868/LifecycleBean.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-2012 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.spr4868; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * @author Sam Brannen + * @since 3.2 + */ +class LifecycleBean { + + private final Log logger = LogFactory.getLog(LifecycleBean.class); + + + @PostConstruct + public void init() { + logger.info("initializing"); + } + + @PreDestroy + public void destroy() { + logger.info("destroying"); + } + +} \ No newline at end of file diff --git a/src/reference/docbook/testing.xml b/src/reference/docbook/testing.xml index 2a52e870c7..1e0602e08e 100644 --- a/src/reference/docbook/testing.xml +++ b/src/reference/docbook/testing.xml @@ -841,6 +841,32 @@ public void testProcessWithoutTransaction() { + + + JSR-250 Lifecycle Annotations + + In the Spring TestContext Framework + @PostConstruct and + @PreDestroy may be used with standard + semantics on any application components configured in the + ApplicationContext; however, these + lifecycle annotations have limited usage within an actual test + class. + + If a method within a test class is annotated with + @PostConstruct, that method will be + executed before any before methods of the + underlying test framework (e.g., methods annotated with JUnit's + @Before), and that will apply for + every test method in the test class. On the other hand, if a method + within a test class is annotated with + @PreDestroy, that method will + never be executed. Within a test + class it is therefore recommended to use test lifecycle callbacks + from the underlying test framework instead of + @PostConstruct and + @PreDestroy. +