Polish "Support expressing application args in @SpringBootTest"

Closes gh-14823
This commit is contained in:
Stephane Nicoll
2019-02-13 15:05:41 +01:00
parent 422e6b7d41
commit 7413584b00
5 changed files with 39 additions and 36 deletions

View File

@@ -6752,18 +6752,20 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
`@SpringBootApplication`) you need to register the `TypeExcludeFilter` with it. See
{dc-spring-boot}/context/TypeExcludeFilter.{dc-ext}[the Javadoc] for details.
[[boot-features-testing-spring-boot-applications-arguments]]
==== Testing with Application Arguments
[[boot-features-testing-spring-boot-application-arguments]]
==== Using Application Arguments
If your application expects <<boot-features-application-arguments,arguments>>, you can
have `@SpringBootTest` inject them using the `args` field.
have `@SpringBootTest` inject them using the `args` attribute.
[source,java,indent=0]
----
include::{test-examples}/context/ApplicationArgumentsExampleTests.java[tag=args]
}
include::{code-examples}/test/context/ApplicationArgumentsExampleTests.java[tag=example]
----
[[boot-features-testing-spring-boot-applications-testing-with-mock-environment]]
==== Testing with a mock environment
By default, `@SpringBootTest` does not start the server. If you have web endpoints that

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,22 +14,21 @@
* limitations under the License.
*/
package org.springframework.boot.docs.context;
package org.springframework.boot.docs.test.context;
// tag::args[]
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
// tag::example[]
@RunWith(SpringRunner.class)
@SpringBootTest(args = { "--foo=bar" })
@SpringBootTest(args = "--app.test=one")
public class ApplicationArgumentsExampleTests {
@Autowired
@@ -37,14 +36,9 @@ public class ApplicationArgumentsExampleTests {
@Test
public void applicationArgumentsPopulated() {
assertThat(this.args.getOptionNames()).contains("foo");
assertThat(this.args.getOptionValues("foo")).contains("bar");
}
// end::args[]
@Configuration
protected static class Config {
assertThat(this.args.getOptionNames()).containsOnly("app.test");
assertThat(this.args.getOptionValues("app.test")).containsOnly("one");
}
}
// end::example[]