Convert remaining samples to use random port
Partial fix for gh-337. See also gh-607 which complements this, but might conflict on a merge.
This commit is contained in:
@@ -364,30 +364,23 @@ that and be sure that it has initialized is to add a `@Bean` of type
|
||||
`ApplicationListener<EmbeddedServletContainerInitializedEvent>` and pull the container
|
||||
out of the event when it is published.
|
||||
|
||||
A really useful thing to do in is to autowire the `EmbeddedWebApplicationContext` into a
|
||||
test case and use it to discover the port that the app is running on. In that way you can
|
||||
use a test profile that chooses a random port (`server.port=0`) and make your test suite
|
||||
independent of its environment. Example:
|
||||
A really useful thing to do in is to use `@IntegrationTest` to set `server.port=0`
|
||||
and then inject the actual ("local") port as a `@Value`. Example:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
|
||||
@WebApplication
|
||||
@IntegrationTest
|
||||
@ActiveProfiles("test")
|
||||
@IntegrationTest("server.port:0")
|
||||
public class CityRepositoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
EmbeddedWebApplicationContext server;
|
||||
|
||||
|
||||
@Value("${local.server.port}")
|
||||
int port;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
port = server.getEmbeddedServletContainer().getPort();
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
|
||||
@@ -199,6 +199,7 @@ sensible overriding of values, properties are considered in the the following or
|
||||
. Command line arguments.
|
||||
. Java System properties (`System.getProperties()`).
|
||||
. OS environment variables.
|
||||
. A `RandomValueProperySource` that only has properties in `random.*`.
|
||||
. `@PropertySource` annotations on your `@Configuration` classes.
|
||||
. Application properties outside of your packaged jar (`application.properties`
|
||||
including YAML and profile variants).
|
||||
@@ -230,8 +231,25 @@ default `name`. When running in production, an `application.properties` can be p
|
||||
outside of your jar that overrides `name`; and for one-off testing, you can launch with
|
||||
a specific command line switch (e.g. `java -jar app.jar --name="Spring"`).
|
||||
|
||||
The `RandomValueProperySource` is useful for injecting random values
|
||||
(e.g. into secrets or test cases). It can produce integers, longs or
|
||||
strings, e.g.
|
||||
|
||||
|
||||
[source,properties,indent=0]
|
||||
----
|
||||
my.secret=${random.value}
|
||||
my.number=${random.int}
|
||||
my.bignumber=${random.long}
|
||||
my.number.less.than.ten=${random.int(10)}
|
||||
my.number.in.range=${random.int[1024,65536]}
|
||||
----
|
||||
|
||||
The `random.int*` syntax is `OPEN value (,max) CLOSE` where the
|
||||
`OPEN,CLOSE` are any character and `value,max` are integers. If
|
||||
`max` is provided then `value` is the minimum value and `max` is the
|
||||
maximum (exclusive).
|
||||
|
||||
[[boot-features-external-config-command-line-args]]
|
||||
=== Accessing command line properties
|
||||
By default `SpringApplication` will convert any command line option arguments (starting
|
||||
@@ -1538,6 +1556,10 @@ interaction. For Example:
|
||||
}
|
||||
----
|
||||
|
||||
To change the port you can add environment properties to
|
||||
`@IntegrationTest` as colon- or equals-separated name-value pairs,
|
||||
e.g. `@IntegrationTest("server.port:9000")`.
|
||||
|
||||
[[boot-features-test-utilities]]
|
||||
=== Test utilities
|
||||
A few test utility classes are packaged as part of `spring-boot` that are generally
|
||||
|
||||
Reference in New Issue
Block a user