Skip to content

Commit d0efc22

Browse files
committed
Support Object property values in MockEnvironment
The setProperty() and withProperty() methods in MockEnvironment were originally introduced with (String, String) signatures; however, they should have always had (String, Object) signatures in order to comply with the MockPropertySource and PropertySource APIs. To address that, this commit introduces variants of these methods that accept Object values for properties. Closes gh-34947
1 parent 6a6abac commit d0efc22

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

spring-test/src/main/java/org/springframework/mock/env/MockEnvironment.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
2626
* @author Chris Beams
2727
* @author Sam Brannen
2828
* @since 3.2
29-
* @see org.springframework.mock.env.MockPropertySource
29+
* @see MockPropertySource
3030
*/
3131
public class MockEnvironment extends AbstractEnvironment {
3232

@@ -43,19 +43,43 @@ public MockEnvironment() {
4343

4444
/**
4545
* Set a property on the underlying {@link MockPropertySource} for this environment.
46+
* @since 6.2.8
47+
* @see MockPropertySource#setProperty(String, Object)
4648
*/
47-
public void setProperty(String key, String value) {
48-
this.propertySource.setProperty(key, value);
49+
public void setProperty(String name, Object value) {
50+
this.propertySource.setProperty(name, value);
4951
}
5052

5153
/**
52-
* Convenient synonym for {@link #setProperty} that returns the current instance.
53-
* Useful for method chaining and fluent-style use.
54+
* Set a property on the underlying {@link MockPropertySource} for this environment.
55+
* @see #setProperty(String, Object)
56+
*/
57+
public void setProperty(String name, String value) {
58+
this.propertySource.setProperty(name, value);
59+
}
60+
61+
/**
62+
* Convenient synonym for {@link #setProperty(String, Object)} that returns
63+
* the current instance.
64+
* <p>Useful for method chaining and fluent-style use.
65+
* @return this {@link MockEnvironment} instance
66+
* @since 6.2.8
67+
* @see MockPropertySource#withProperty(String, Object)
68+
*/
69+
public MockEnvironment withProperty(String name, Object value) {
70+
setProperty(name, value);
71+
return this;
72+
}
73+
74+
/**
75+
* Convenient synonym for {@link #setProperty(String, String)} that returns
76+
* the current instance.
77+
* <p>Useful for method chaining and fluent-style use.
5478
* @return this {@link MockEnvironment} instance
55-
* @see MockPropertySource#withProperty
79+
* @see #withProperty(String, Object)
5680
*/
57-
public MockEnvironment withProperty(String key, String value) {
58-
setProperty(key, value);
81+
public MockEnvironment withProperty(String name, String value) {
82+
setProperty(name, value);
5983
return this;
6084
}
6185

0 commit comments

Comments
 (0)