Browse Source

Verify ServletCtxAware beans are processed in WAC tests

SPR-11145 claims that ServletContextAware beans declared in an
ApplicationContext loaded for an integration test by the TestContext
framework (TCF) do not have their setServletContext() methods invoked
if the tests are executed manually using JUnit 4.11.

This commit verifies that such ServletContextAware beans are processed
properly regardless of how the test was launched. Specifically:

 - A ServletContextAwareBean has been introduced.

 - BasicAnnotationConfigWacTests has been retrofitted with a
   ServletContextAwareBean in its context.

 - ServletContextAwareBeanWacTests has been introduced to execute
   BasicAnnotationConfigWacTests manually via JUnitCore.

Issue: SPR-11145
pull/455/head
Sam Brannen 12 years ago
parent
commit
a521ef5cee
  1. 20
      spring-test/src/test/java/org/springframework/test/context/web/BasicAnnotationConfigWacTests.java
  2. 38
      spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBean.java
  3. 49
      spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBeanWacTests.java

20
spring-test/src/test/java/org/springframework/test/context/web/BasicAnnotationConfigWacTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -16,13 +16,14 @@ @@ -16,13 +16,14 @@
package org.springframework.test.context.web;
import static org.junit.Assert.*;
import org.junit.Test;
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 static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 3.2
@ -37,12 +38,25 @@ public class BasicAnnotationConfigWacTests extends AbstractBasicWacTests { @@ -37,12 +38,25 @@ public class BasicAnnotationConfigWacTests extends AbstractBasicWacTests {
public String foo() {
return "enigma";
}
@Bean
public ServletContextAwareBean servletContextAwareBean() {
return new ServletContextAwareBean();
}
}
@Autowired
protected ServletContextAwareBean servletContextAwareBean;
@Test
public void fooEnigmaAutowired() {
assertEquals("enigma", foo);
}
@Test
public void servletContextAwareBeanProcessed() {
assertNotNull(servletContextAwareBean);
assertNotNull(servletContextAwareBean.servletContext);
}
}

38
spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBean.java

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
/*
* Copyright 2002-2014 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.web;
import javax.servlet.ServletContext;
import org.springframework.web.context.ServletContextAware;
/**
* Introduced to investigate claims in SPR-11145.
*
* @author Sam Brannen
* @since 4.0.2
*/
public class ServletContextAwareBean implements ServletContextAware {
protected ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
}

49
spring-test/src/test/java/org/springframework/test/context/web/ServletContextAwareBeanWacTests.java

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
/*
* Copyright 2002-2014 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.web;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.springframework.test.context.junit4.TrackingRunListener;
import static org.junit.Assert.*;
/**
* Introduced to investigate claims in SPR-11145.
*
* <p>
* Yes, this test class does in fact use JUnit to run JUnit. ;)
*
* @author Sam Brannen
* @since 4.0.2
*/
public class ServletContextAwareBeanWacTests {
@Test
public void ensureServletContextAwareBeanIsProcessedProperlyWhenExecutingJUnitManually() {
TrackingRunListener listener = new TrackingRunListener();
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(BasicAnnotationConfigWacTests.class);
assertEquals(3, listener.getTestStartedCount());
assertEquals(3, listener.getTestFinishedCount());
assertEquals(0, listener.getTestFailureCount());
}
}
Loading…
Cancel
Save