Skip to content

Commit 422e6b7

Browse files
Justin Griffinsnicoll
authored andcommitted
Support expressing application args in @SpringBootTest
Add `args` property to the `@SpringBootTest` annotation so tests can easily supply application arguments to pass to their app under test. See gh-14823
1 parent 1f08a52 commit 422e6b7

File tree

5 files changed

+134
-1
lines changed

5 files changed

+134
-1
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6752,6 +6752,17 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
67526752
`@SpringBootApplication`) you need to register the `TypeExcludeFilter` with it. See
67536753
{dc-spring-boot}/context/TypeExcludeFilter.{dc-ext}[the Javadoc] for details.
67546754

6755+
[[boot-features-testing-spring-boot-applications-arguments]]
6756+
==== Testing with Application Arguments
6757+
6758+
If your application expects <<boot-features-application-arguments,arguments>>, you can
6759+
have `@SpringBootTest` inject them using the `args` field.
6760+
6761+
[source,java,indent=0]
6762+
----
6763+
include::{test-examples}/context/ApplicationArgumentsExampleTests.java[tag=args]
6764+
}
6765+
----
67556766

67566767
[[boot-features-testing-spring-boot-applications-testing-with-mock-environment]]
67576768
==== Testing with a mock environment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.context;
18+
19+
// tag::args[]
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.ApplicationArguments;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.junit4.SpringRunner;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
@RunWith(SpringRunner.class)
32+
@SpringBootTest(args = { "--foo=bar" })
33+
public class ApplicationArgumentsExampleTests {
34+
35+
@Autowired
36+
private ApplicationArguments args;
37+
38+
@Test
39+
public void applicationArgumentsPopulated() {
40+
assertThat(this.args.getOptionNames()).contains("foo");
41+
assertThat(this.args.getOptionValues("foo")).contains("bar");
42+
}
43+
44+
// end::args[]
45+
@Configuration
46+
protected static class Config {
47+
48+
}
49+
50+
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ else if (config instanceof ReactiveWebMergedContextConfiguration) {
124124
application.setWebApplicationType(WebApplicationType.NONE);
125125
}
126126
application.setInitializers(initializers);
127-
return application.run();
127+
return application.run(getArgs(config));
128128
}
129129

130130
/**
@@ -145,6 +145,19 @@ protected ConfigurableEnvironment getEnvironment() {
145145
return new StandardEnvironment();
146146
}
147147

148+
/**
149+
* Get the {@link SpringBootTest#args()} (if present) specified in the annotated test
150+
* class. If no args given, returns empty array.
151+
* @param config the source context configuration
152+
* @return the {@link SpringBootTest#args()} (if present) specified in the annotated
153+
* test class, or empty array
154+
*/
155+
protected String[] getArgs(MergedContextConfiguration config) {
156+
SpringBootTest annotation = AnnotatedElementUtils
157+
.findMergedAnnotation(config.getTestClass(), SpringBootTest.class);
158+
return (annotation != null) ? annotation.args() : new String[0];
159+
}
160+
148161
private void setActiveProfiles(ConfigurableEnvironment environment,
149162
String[] profiles) {
150163
TestPropertyValues

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
*/
107107
Class<?>[] classes() default {};
108108

109+
/**
110+
* Arguments that should be passed to the application under test.
111+
* @return the arguments to pass to the application under test.
112+
*/
113+
String[] args() default {};
114+
109115
/**
110116
* The type of web environment to create when applicable. Defaults to
111117
* {@link WebEnvironment#MOCK}.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.ApplicationArguments;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.test.context.junit4.SpringRunner;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Assert that tests annotated with {@link SpringBootTest} can specify
31+
* {@link SpringBootTest#args()} to be passed to their application under test.
32+
*
33+
* @author Justin Griffin
34+
*/
35+
@RunWith(SpringRunner.class)
36+
@SpringBootTest(args = { "--option.foo=option-foo-value", "other.bar=other-bar-value" })
37+
public class SpringBootTestArgsTests {
38+
39+
@Autowired
40+
private ApplicationArguments args;
41+
42+
@Test
43+
public void applicationArgumentsPopulated() {
44+
assertThat(this.args.getOptionNames()).contains("option.foo");
45+
assertThat(this.args.getNonOptionArgs()).contains("other.bar=other-bar-value");
46+
}
47+
48+
@Configuration
49+
protected static class Config {
50+
51+
}
52+
53+
}

0 commit comments

Comments
 (0)