Polish "Support expressing application args in @SpringBootTest"
Closes gh-14823
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.docs.test.context;
|
||||
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
// tag::example[]
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(args = "--app.test=one")
|
||||
public class ApplicationArgumentsExampleTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationArguments args;
|
||||
|
||||
@Test
|
||||
public void applicationArgumentsPopulated() {
|
||||
assertThat(this.args.getOptionNames()).containsOnly("app.test");
|
||||
assertThat(this.args.getOptionValues("app.test")).containsOnly("one");
|
||||
}
|
||||
|
||||
}
|
||||
// end::example[]
|
||||
Reference in New Issue
Block a user