Spring MockPropertySource

withProperty

  • 设置属性名称和属性值
1
2
3
4
public MockPropertySource withProperty(String name, Object value) {
   this.setProperty(name, value);
   return this;
}

setProperty

1
2
3
public void setProperty(String name, Object value) {
   this.source.put(name, value);
}

完整代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class MockPropertySource extends PropertiesPropertySource {

   /**
    * {@value} is the default name for {@link MockPropertySource} instances not
    * otherwise given an explicit name.
    * @see #MockPropertySource()
    * @see #MockPropertySource(String)
    */
   public static final String MOCK_PROPERTIES_PROPERTY_SOURCE_NAME = "mockProperties";

   /**
    * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
    * that will maintain its own internal {@link Properties} instance.
    */
   public MockPropertySource() {
      this(new Properties());
   }

   /**
    * Create a new {@code MockPropertySource} with the given name that will
    * maintain its own internal {@link Properties} instance.
    * @param name the {@linkplain #getName() name} of the property source
    */
   public MockPropertySource(String name) {
      this(name, new Properties());
   }

   /**
    * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
    * and backed by the given {@link Properties} object.
    * @param properties the properties to use
    */
   public MockPropertySource(Properties properties) {
      this(MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, properties);
   }

   /**
    * 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
    */
   public MockPropertySource(String name, Properties properties) {
      super(name, properties);
   }

   /**
    * Set the given property on the underlying {@link Properties} object.
    */
   public void setProperty(String name, Object value) {
      // map 操作
      this.source.put(name, value);
   }

   /**
    * Convenient synonym for {@link #setProperty} that returns the current instance.
    * Useful for method chaining and fluent-style use.
    * 设置属性名称和属性值
    * @return this {@link MockPropertySource} instance
    */
      public MockPropertySource withProperty(String name, Object value) {
         this.setProperty(name, value);
         return this;
      }

}