diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 6b7b584091..a54e51f0e8 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -410,13 +410,14 @@ or as a JNDI variable `java:comp/env/spring.application.json`. [[boot-features-external-config-random-values]] === Configuring random values The `RandomValuePropertySource` is useful for injecting random values (e.g. into secrets -or test cases). It can produce integers, longs or strings, e.g. +or test cases). It can produce integers, longs, uuids or strings, e.g. [source,properties,indent=0] ---- my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} + my.uuid=${random.uuid} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]} ---- diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java b/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java index 82b067b1a7..26a755de97 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java @@ -17,6 +17,7 @@ package org.springframework.boot.context.config; import java.util.Random; +import java.util.UUID; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -94,6 +95,9 @@ public class RandomValuePropertySource extends PropertySource { if (range != null) { return getNextLongInRange(range); } + if (type.equals("uuid")) { + return UUID.randomUUID().toString(); + } return getRandomBytes(); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java index 45ca29b681..2941aa11b5 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.context.config; import java.util.Random; +import java.util.UUID; import org.junit.Test; import org.mockito.Mockito; @@ -49,6 +50,13 @@ public class RandomValuePropertySourceTests { assertThat(value).isNotNull(); } + @Test + public void uuidValue() { + String value = (String) this.source.getProperty("random.uuid"); + assertThat(value).isNotNull(); + assertThat(UUID.fromString(value)).isNotNull(); + } + @Test public void intRange() { Integer value = (Integer) this.source.getProperty("random.int[4,10]");