Introduce MockEnvironment in the spring-test module

For legacy reasons, a MockEnvironment implementation already exists in multiple places within Spring's test suite; however, it is not available to the general public.

This commit promotes MockEnvironment to a first-class citizen in the spring-test module, alongside the existing MockPropertySource.

In addition, the following house cleaning has been performed.

 - deleted MockPropertySource from the spring-expression module
 - deleted MockEnvironment from the "spring" integration testing module
 - updated test copies of MockPropertySource and MockEnvironment
 - documented MockEnvironment and MockPropertySource in the testing
   chapter of the reference manual

Issue: SPR-9492
This commit is contained in:
Sam Brannen
2012-07-28 20:10:21 +02:00
parent 0329df218e
commit f49b22c78f
9 changed files with 72 additions and 143 deletions

View File

@@ -0,0 +1,61 @@
/*
* 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.mock.env;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* Simple {@link ConfigurableEnvironment} implementation exposing
* {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
* methods for testing purposes.
*
* @author Chris Beams
* @author Sam Brannen
* @since 3.2
* @see org.springframework.mock.env.MockPropertySource
*/
public class MockEnvironment extends AbstractEnvironment {
private MockPropertySource propertySource = new MockPropertySource();
/**
* Create a new {@code MockEnvironment} with a single {@link MockPropertySource}.
*/
public MockEnvironment() {
getPropertySources().addLast(propertySource);
}
/**
* Set a property on the underlying {@link MockPropertySource} for this environment.
*/
public void setProperty(String key, String value) {
propertySource.setProperty(key, value);
}
/**
* Convenient synonym for {@link #setProperty} that returns the current instance.
* Useful for method chaining and fluent-style use.
* @return this {@link MockEnvironment} instance
* @see MockPropertySource#withProperty(String, String)
*/
public MockEnvironment withProperty(String key, String value) {
this.setProperty(key, value);
return this;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* 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.
@@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource;
*
* @author Chris Beams
* @since 3.1
* @see MockEnvironment
* @see org.springframework.mock.env.MockEnvironment
*/
public class MockPropertySource extends PropertiesPropertySource {
@@ -75,8 +75,8 @@ public class MockPropertySource extends PropertiesPropertySource {
}
/**
* Create a new {@code MockPropertySource} with with the given name and backed by the given
* {@link Properties} object
* Create a new {@code MockPropertySource} with the given name and backed by the given
* {@link Properties} object.
* @param name the {@linkplain #getName() name} of the property source
* @param properties the properties to use
*/
@@ -100,4 +100,5 @@ public class MockPropertySource extends PropertiesPropertySource {
this.setProperty(name, value);
return this;
}
}

View File

@@ -0,0 +1,10 @@
/**
* This package contains mock implementations of the
* {@link org.springframework.core.env.Environment Environment} and
* {@link org.springframework.core.env.PropertySource PropertySource}
* abstractions introduced in Spring 3.1.
*
* <p>These <em>mocks</em> are useful for developing <em>out-of-container</em>
* unit tests for code that depends on environment-specific properties.
*/
package org.springframework.mock.env;