Spring MockPropertySource

withProperty

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

setProperty

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

完整代码

  1. public class MockPropertySource extends PropertiesPropertySource {
  2. /**
  3. * {@value} is the default name for {@link MockPropertySource} instances not
  4. * otherwise given an explicit name.
  5. * @see #MockPropertySource()
  6. * @see #MockPropertySource(String)
  7. */
  8. public static final String MOCK_PROPERTIES_PROPERTY_SOURCE_NAME = "mockProperties";
  9. /**
  10. * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
  11. * that will maintain its own internal {@link Properties} instance.
  12. */
  13. public MockPropertySource() {
  14. this(new Properties());
  15. }
  16. /**
  17. * Create a new {@code MockPropertySource} with the given name that will
  18. * maintain its own internal {@link Properties} instance.
  19. * @param name the {@linkplain #getName() name} of the property source
  20. */
  21. public MockPropertySource(String name) {
  22. this(name, new Properties());
  23. }
  24. /**
  25. * Create a new {@code MockPropertySource} named {@value #MOCK_PROPERTIES_PROPERTY_SOURCE_NAME}
  26. * and backed by the given {@link Properties} object.
  27. * @param properties the properties to use
  28. */
  29. public MockPropertySource(Properties properties) {
  30. this(MOCK_PROPERTIES_PROPERTY_SOURCE_NAME, properties);
  31. }
  32. /**
  33. * Create a new {@code MockPropertySource} with the given name and backed by the given
  34. * {@link Properties} object.
  35. * @param name the {@linkplain #getName() name} of the property source
  36. * @param properties the properties to use
  37. */
  38. public MockPropertySource(String name, Properties properties) {
  39. super(name, properties);
  40. }
  41. /**
  42. * Set the given property on the underlying {@link Properties} object.
  43. */
  44. public void setProperty(String name, Object value) {
  45. // map 操作
  46. this.source.put(name, value);
  47. }
  48. /**
  49. * Convenient synonym for {@link #setProperty} that returns the current instance.
  50. * Useful for method chaining and fluent-style use.
  51. * 设置属性名称和属性值
  52. * @return this {@link MockPropertySource} instance
  53. */
  54. public MockPropertySource withProperty(String name, Object value) {
  55. this.setProperty(name, value);
  56. return this;
  57. }
  58. }