Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Introduce MockEnvironment in the spring-test module #111

Merged
merged 1 commit into from over 1 year ago

1 participant

Sam Brannen
Sam Brannen
Collaborator

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

Sam Brannen 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
f49b22c
Sam Brannen sbrannen merged commit d217855 into from July 28, 2012
Sam Brannen sbrannen closed this July 28, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jul 28, 2012
Sam Brannen 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
f49b22c
This page is out of date. Refresh to see the latest.
9  spring-context/src/test/java/org/springframework/mock/env/MockEnvironment.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2011 the original author or authors.
  2
+ * Copyright 2002-2012 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -20,13 +20,14 @@
20 20
 import org.springframework.core.env.ConfigurableEnvironment;
21 21
 
22 22
 /**
23  
- * Simple {@link ConfigurableEnvironment} implementation exposing a
  23
+ * Simple {@link ConfigurableEnvironment} implementation exposing
24 24
  * {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
25 25
  * methods for testing purposes.
26 26
  * 
27 27
  * @author Chris Beams
28  
- * @since 3.1
29  
- * @see MockPropertySource
  28
+ * @author Sam Brannen
  29
+ * @since 3.2
  30
+ * @see org.springframework.mock.env.MockPropertySource
30 31
  */
31 32
 public class MockEnvironment extends AbstractEnvironment {
32 33
 
7  spring-context/src/test/java/org/springframework/mock/env/MockPropertySource.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2011 the original author or authors.
  2
+ * Copyright 2002-2012 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
36 36
  *
37 37
  * @author Chris Beams
38 38
  * @since 3.1
39  
- * @see MockEnvironment
  39
+ * @see org.springframework.mock.env.MockEnvironment
40 40
  */
41 41
 public class MockPropertySource extends PropertiesPropertySource {
42 42
 
@@ -75,7 +75,7 @@ public MockPropertySource(Properties properties) {
75 75
 	}
76 76
 
77 77
 	/**
78  
-	 * Create a new {@code MockPropertySource} with with the given name and backed by the given
  78
+	 * Create a new {@code MockPropertySource} with the given name and backed by the given
79 79
 	 * {@link Properties} object
80 80
 	 * @param name the {@linkplain #getName() name} of the property source
81 81
 	 * @param properties the properties to use
@@ -100,4 +100,5 @@ public MockPropertySource withProperty(String name, Object value) {
100 100
 		this.setProperty(name, value);
101 101
 		return this;
102 102
 	}
  103
+
103 104
 }
7  spring-core/src/test/java/org/springframework/mock/env/MockPropertySource.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2011 the original author or authors.
  2
+ * Copyright 2002-2012 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
36 36
  *
37 37
  * @author Chris Beams
38 38
  * @since 3.1
39  
- * @see MockEnvironment
  39
+ * @see org.springframework.mock.env.MockEnvironment
40 40
  */
41 41
 public class MockPropertySource extends PropertiesPropertySource {
42 42
 
@@ -75,7 +75,7 @@ public MockPropertySource(Properties properties) {
75 75
 	}
76 76
 
77 77
 	/**
78  
-	 * Create a new {@code MockPropertySource} with with the given name and backed by the given
  78
+	 * Create a new {@code MockPropertySource} with the given name and backed by the given
79 79
 	 * {@link Properties} object
80 80
 	 * @param name the {@linkplain #getName() name} of the property source
81 81
 	 * @param properties the properties to use
@@ -100,4 +100,5 @@ public MockPropertySource withProperty(String name, Object value) {
100 100
 		this.setProperty(name, value);
101 101
 		return this;
102 102
 	}
  103
+
103 104
 }
102  spring-expression/src/test/java/org/springframework/mock/env/MockPropertySource.java
... ...
@@ -1,102 +0,0 @@
1  
-/*
2  
- * Copyright 2002-2011 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.mock.env;
18  
-
19  
-import java.util.Properties;
20  
-
21  
-import org.springframework.core.env.PropertiesPropertySource;
22  
-import org.springframework.core.env.PropertySource;
23  
-
24  
-/**
25  
- * Simple {@link PropertySource} implementation for use in testing. Accepts
26  
- * a user-provided {@link Properties} object, or if omitted during construction,
27  
- * the implementation will initialize its own.
28  
- *
29  
- * The {@link #setProperty} and {@link #withProperty} methods are exposed for
30  
- * convenience, for example:
31  
- * <pre>
32  
- * {@code
33  
- *   PropertySource<?> source = new MockPropertySource().withProperty("foo", "bar");
34  
- * }
35  
- * </pre>
36  
- *
37  
- * @author Chris Beams
38  
- * @since 3.1
39  
- */
40  
-public class MockPropertySource extends PropertiesPropertySource {
41  
-
42  
-	/**
43  
-	 * {@value} is the default name for {@link MockPropertySource} instances not
44  
-	 * otherwise given an explicit name.
45  
-	 * @see #MockPropertySource()
46  
-	 * @see #MockPropertySource(String)
47  
-	 */
48  
-	public static final String MOCK_PROPERTIES_PROPERTY_SOURCE_NAME = "mockProperties";
49  
-
50  
-	/**
51  
-	 * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
52  
-	 * that will maintain its own internal {@link Properties} instance.
53  
-	 */
54  
-	public MockPropertySource() {
55  
-		this(new Properties());
56  
-	}
57  
-
58  
-	/**
59  
-	 * Create a new {@code MockPropertySource} with the given name that will
60  
-	 * maintain its own internal {@link Properties} instance.
61  
-	 * @param name the {@linkplain #getName() name} of the property source
62  
-	 */
63  
-	public MockPropertySource(String name) {
64  
-		this(name, new Properties());
65  
-	}
66  
-
67  
-	/**
68  
-	 * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
69  
-	 * and backed by the given {@link Properties} object.
70  
-	 * @param properties the properties to use
71  
-	 */
72  
-	public MockPropertySource(Properties properties) {
73  
-		this(MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, properties);
74  
-	}
75  
-
76  
-	/**
77  
-	 * Create a new {@code MockPropertySource} with with the given name and backed by the given
78  
-	 * {@link Properties} object
79  
-	 * @param name the {@linkplain #getName() name} of the property source
80  
-	 * @param properties the properties to use
81  
-	 */
82  
-	public MockPropertySource(String name, Properties properties) {
83  
-		super(name, properties);
84  
-	}
85  
-
86  
-	/**
87  
-	 * Set the given property on the underlying {@link Properties} object.
88  
-	 */
89  
-	public void setProperty(String name, Object value) {
90  
-		this.source.put(name, value);
91  
-	}
92  
-
93  
-	/**
94  
-	 * Convenient synonym for {@link #setProperty} that returns the current instance.
95  
-	 * Useful for method chaining and fluent-style use.
96  
-	 * @return this {@link MockPropertySource} instance
97  
-	 */
98  
-	public MockPropertySource withProperty(String name, Object value) {
99  
-		this.setProperty(name, value);
100  
-		return this;
101  
-	}
102  
-}
9  ...org/springframework/mock/env/MockEnvironment.java → ...org/springframework/mock/env/MockEnvironment.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2011 the original author or authors.
  2
+ * Copyright 2002-2012 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -20,13 +20,14 @@
20 20
 import org.springframework.core.env.ConfigurableEnvironment;
21 21
 
22 22
 /**
23  
- * Simple {@link ConfigurableEnvironment} implementation exposing a
  23
+ * Simple {@link ConfigurableEnvironment} implementation exposing
24 24
  * {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
25 25
  * methods for testing purposes.
26 26
  * 
27 27
  * @author Chris Beams
28  
- * @since 3.1
29  
- * @see MockPropertySource
  28
+ * @author Sam Brannen
  29
+ * @since 3.2
  30
+ * @see org.springframework.mock.env.MockPropertySource
30 31
  */
31 32
 public class MockEnvironment extends AbstractEnvironment {
32 33
 
9  spring-test/src/main/java/org/springframework/mock/env/MockPropertySource.java
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2  
- * Copyright 2002-2011 the original author or authors.
  2
+ * Copyright 2002-2012 the original author or authors.
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
36 36
  *
37 37
  * @author Chris Beams
38 38
  * @since 3.1
39  
- * @see MockEnvironment
  39
+ * @see org.springframework.mock.env.MockEnvironment
40 40
  */
41 41
 public class MockPropertySource extends PropertiesPropertySource {
42 42
 
@@ -75,8 +75,8 @@ public MockPropertySource(Properties properties) {
75 75
 	}
76 76
 
77 77
 	/**
78  
-	 * Create a new {@code MockPropertySource} with with the given name and backed by the given
79  
-	 * {@link Properties} object
  78
+	 * Create a new {@code MockPropertySource} with the given name and backed by the given
  79
+	 * {@link Properties} object.
80 80
 	 * @param name the {@linkplain #getName() name} of the property source
81 81
 	 * @param properties the properties to use
82 82
 	 */
@@ -100,4 +100,5 @@ public MockPropertySource withProperty(String name, Object value) {
100 100
 		this.setProperty(name, value);
101 101
 		return this;
102 102
 	}
  103
+
103 104
 }
10  spring-test/src/main/java/org/springframework/mock/env/package-info.java
... ...
@@ -0,0 +1,10 @@
  1
+/**
  2
+ * This package contains mock implementations of the
  3
+ * {@link org.springframework.core.env.Environment Environment} and
  4
+ * {@link org.springframework.core.env.PropertySource PropertySource}
  5
+ * abstractions introduced in Spring 3.1.
  6
+ * 
  7
+ * <p>These <em>mocks</em> are useful for developing <em>out-of-container</em>
  8
+ * unit tests for code that depends on environment-specific properties.
  9
+ */
  10
+package org.springframework.mock.env;
46  src/dist/changelog.txt
@@ -7,29 +7,29 @@ Changes in version 3.2 M2 (2012-08-xx)
7 7
 --------------------------------------
8 8
 
9 9
 * spring-test module now depends on junit:junit-dep (SPR-6966)
10  
-* infer return type of parameterized factory methods (SPR-9493)
11  
-* used BufferedInputStream in SimpleMetaDataReader to double performance (SPR-9528)
12  
-* added "repeatCount" bean property to Quartz SimpleTriggerFactoryBean (SPR-9521)
13  
-* added "jtaTransactionManager" property to Hibernate 4 LocalSessionFactoryBean/Builder (SPR-9480)
14  
-* raise RestClientException instead of IllegalArgumentException for unknown status codes
15  
-* added "defaultCharset" property to StringHttpMessageConverter (SPR-9487)
16  
-* added JacksonObjectMapperFactoryBean for configuring a Jackson ObjectMapper in XML
17  
-* added ContentNegotiationManager/ContentNegotiationStrategy to resolve requested media types
18  
-* added support for the HTTP PATCH method to Spring MVC and to RestTemplate (SPR-7985)
19  
-* enable smart suffix pattern match in @RequestMapping methods (SPR-7632)
20  
-* DispatcherPortlet does not forward event exceptions to the render phase by default (SPR-9287)
21  
-* add defaultCharset property to StringHttpMessageConverter
22  
-* add @ExceptionResolver annotation to detect classes with @ExceptionHandler methods
23  
-* move RSS/Atom message converter registration ahead of jackson/jaxb2
24  
-* handle BindException in DefaultHandlerExceptionResolver
25  
-* parameterize DeferredResult type
26  
-* use reflection to instantiate StandardServletAsyncWebRequest
27  
-* fix issue with forward before async request processing
28  
-* add exclude patterns for mapped interceptors in MVC namespace and MVC Java config
29  
-* support content negotiation options in MVC namespace and MVC Java config
30  
-* support named dispatchers in MockServletContext (SPR-9587)
31  
-* support single, unqualified tx manager in the TestContext framework (SPR-9645)
32  
-* support TransactionManagementConfigurer in the TestContext framework (SPR-9604)
  10
+* now inferring return type of parameterized factory methods (SPR-9493)
  11
+* now using BufferedInputStream in SimpleMetaDataReader to double performance (SPR-9528)
  12
+* introduced "repeatCount" property in Quartz SimpleTriggerFactoryBean (SPR-9521)
  13
+* introduced "jtaTransactionManager" property in Hibernate 4 LocalSessionFactoryBean/Builder (SPR-9480)
  14
+* now raising RestClientException instead of IllegalArgumentException for unknown status codes
  15
+* introduced JacksonObjectMapperFactoryBean for configuring a Jackson ObjectMapper in XML
  16
+* introduced ContentNegotiationManager/ContentNegotiationStrategy for resolving requested media types
  17
+* introduced support for the HTTP PATCH method in Spring MVC and RestTemplate (SPR-7985)
  18
+* enabled smart suffix pattern matching in @RequestMapping methods (SPR-7632)
  19
+* DispatcherPortlet no longer forwards event exceptions to the render phase by default (SPR-9287)
  20
+* introduced "defaultCharset" property in StringHttpMessageConverter (SPR-9487)
  21
+* introduced @ExceptionResolver annotation for detecting classes with @ExceptionHandler methods
  22
+* moved RSS/Atom message converter registration ahead of jackson/jaxb2
  23
+* now handling BindException in DefaultHandlerExceptionResolver
  24
+* DeferredResult type is now parameterized
  25
+* now using reflection to instantiate StandardServletAsyncWebRequest
  26
+* fixed issue with forward before async request processing
  27
+* introduced exclude patterns for mapped interceptors in MVC namespace and MVC Java config
  28
+* introduced support for content negotiation options in MVC namespace and MVC Java config
  29
+* introduced support for named dispatchers in MockServletContext (SPR-9587)
  30
+* introduced support for single, unqualified tx manager in the TestContext framework (SPR-9645)
  31
+* introduced support for TransactionManagementConfigurer in the TestContext framework (SPR-9604)
  32
+* introduced MockEnvironment in the spring-test module (SPR-9492)
33 33
 
34 34
 
35 35
 Changes in version 3.2 M1 (2012-05-28)
16  src/reference/docbook/testing.xml
@@ -47,6 +47,22 @@
47 47
     <section xml:id="mock-objects">
48 48
       <title>Mock Objects</title>
49 49
 
  50
+      <section xml:id="mock-objects-env">
  51
+        <title>Environment</title>
  52
+
  53
+        <para>The <literal>org.springframework.mock.env</literal> package
  54
+        contains mock implementations of the
  55
+        <interfacename>Environment</interfacename> and
  56
+        <interfacename>PropertySource</interfacename> abstractions introduced
  57
+        in Spring 3.1 (see <xref
  58
+        linkend="new-in-3.1-environment-abstraction" /> and <xref
  59
+        linkend="new-in-3.1-property-source-abstraction" />).
  60
+        <classname>MockEnvironment</classname> and
  61
+        <classname>MockPropertySource</classname> are useful for developing
  62
+        <emphasis>out-of-container</emphasis> unit tests for code that depends
  63
+        on environment-specific properties.</para>
  64
+      </section>
  65
+
50 66
       <section xml:id="mock-objects-jndi">
51 67
         <title>JNDI</title>
52 68
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.