Skip to content

Commit c8070f4

Browse files
committed
Merge pull request #14823 from jgriff
* pr/14823: Polish "Support expressing application `args` in `@SpringBootTest`" Support expressing application `args` in `@SpringBootTest`
2 parents 1f08a52 + 7413584 commit c8070f4

File tree

5 files changed

+139
-3
lines changed

5 files changed

+139
-3
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6753,6 +6753,19 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
67536753
{dc-spring-boot}/context/TypeExcludeFilter.{dc-ext}[the Javadoc] for details.
67546754

67556755

6756+
6757+
[[boot-features-testing-spring-boot-application-arguments]]
6758+
==== Using Application Arguments
6759+
If your application expects <<boot-features-application-arguments,arguments>>, you can
6760+
have `@SpringBootTest` inject them using the `args` attribute.
6761+
6762+
[source,java,indent=0]
6763+
----
6764+
include::{code-examples}/test/context/ApplicationArgumentsExampleTests.java[tag=example]
6765+
----
6766+
6767+
6768+
67566769
[[boot-features-testing-spring-boot-applications-testing-with-mock-environment]]
67576770
==== Testing with a mock environment
67586771
By default, `@SpringBootTest` does not start the server. If you have web endpoints that
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2019 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.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.boot.test.context.SpringBootTest;
25+
import org.springframework.test.context.junit4.SpringRunner;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
// tag::example[]
30+
@RunWith(SpringRunner.class)
31+
@SpringBootTest(args = "--app.test=one")
32+
public class ApplicationArgumentsExampleTests {
33+
34+
@Autowired
35+
private ApplicationArguments args;
36+
37+
@Test
38+
public void applicationArgumentsPopulated() {
39+
assertThat(this.args.getOptionNames()).containsOnly("app.test");
40+
assertThat(this.args.getOptionValues("app.test")).containsOnly("one");
41+
}
42+
43+
}
44+
// end::example[]

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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+
* Return the application arguments to use. If no arguments are available, return an
150+
* empty array.
151+
* @param config the source context configuration
152+
* @return the application arguments to use
153+
* @see SpringApplication#run(String...)
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525

2626
import org.junit.jupiter.api.extension.ExtendWith;
2727

28+
import org.springframework.boot.ApplicationArguments;
2829
import org.springframework.boot.SpringApplication;
2930
import org.springframework.boot.SpringBootConfiguration;
3031
import org.springframework.boot.WebApplicationType;
@@ -55,6 +56,8 @@
5556
* specified.</li>
5657
* <li>Allows custom {@link Environment} properties to be defined using the
5758
* {@link #properties() properties attribute}.</li>
59+
* <li>Allows application arguments to be defined using the {@link #args() args
60+
* attribute}.</li>
5861
* <li>Provides support for different {@link #webEnvironment() webEnvironment} modes,
5962
* including the ability to start a fully running web server listening on a
6063
* {@link WebEnvironment#DEFINED_PORT defined} or {@link WebEnvironment#RANDOM_PORT
@@ -93,6 +96,14 @@
9396
@AliasFor("value")
9497
String[] properties() default {};
9598

99+
/**
100+
* Application arguments that should be passed to the application under test.
101+
* @return the application arguments to pass to the application under test.
102+
* @see ApplicationArguments
103+
* @see SpringApplication#run(String...)
104+
*/
105+
String[] args() default {};
106+
96107
/**
97108
* The <em>annotated classes</em> to use for loading an
98109
* {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 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+
* Tests for {@link SpringBootTest} with application arguments.
31+
*
32+
* @author Justin Griffin
33+
* @author Stephane Nicoll
34+
*/
35+
@RunWith(SpringRunner.class)
36+
@SpringBootTest(args = { "--option.foo=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()).containsOnly("option.foo");
45+
assertThat(this.args.getOptionValues("option.foo")).containsOnly("foo-value");
46+
assertThat(this.args.getNonOptionArgs())
47+
.containsOnly("other.bar=other-bar-value");
48+
}
49+
50+
@Configuration
51+
protected static class Config {
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)