Skip to content

Commit 7413584

Browse files
committed
Polish "Support expressing application args in @SpringBootTest"
Closes gh-14823
1 parent 422e6b7 commit 7413584

File tree

5 files changed

+39
-36
lines changed

5 files changed

+39
-36
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6752,18 +6752,20 @@ 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
67576755

6756+
6757+
[[boot-features-testing-spring-boot-application-arguments]]
6758+
==== Using Application Arguments
67586759
If your application expects <<boot-features-application-arguments,arguments>>, you can
6759-
have `@SpringBootTest` inject them using the `args` field.
6760+
have `@SpringBootTest` inject them using the `args` attribute.
67606761

67616762
[source,java,indent=0]
67626763
----
6763-
include::{test-examples}/context/ApplicationArgumentsExampleTests.java[tag=args]
6764-
}
6764+
include::{code-examples}/test/context/ApplicationArgumentsExampleTests.java[tag=example]
67656765
----
67666766

6767+
6768+
67676769
[[boot-features-testing-spring-boot-applications-testing-with-mock-environment]]
67686770
==== Testing with a mock environment
67696771
By default, `@SpringBootTest` does not start the server. If you have web endpoints that
Lines changed: 7 additions & 13 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.
@@ -14,37 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.docs.context;
17+
package org.springframework.boot.docs.test.context;
1818

19-
// tag::args[]
2019
import org.junit.Test;
2120
import org.junit.runner.RunWith;
2221

2322
import org.springframework.beans.factory.annotation.Autowired;
2423
import org.springframework.boot.ApplicationArguments;
2524
import org.springframework.boot.test.context.SpringBootTest;
26-
import org.springframework.context.annotation.Configuration;
2725
import org.springframework.test.context.junit4.SpringRunner;
2826

2927
import static org.assertj.core.api.Assertions.assertThat;
3028

29+
// tag::example[]
3130
@RunWith(SpringRunner.class)
32-
@SpringBootTest(args = { "--foo=bar" })
31+
@SpringBootTest(args = "--app.test=one")
3332
public class ApplicationArgumentsExampleTests {
3433

3534
@Autowired
3635
private ApplicationArguments args;
3736

3837
@Test
3938
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-
39+
assertThat(this.args.getOptionNames()).containsOnly("app.test");
40+
assertThat(this.args.getOptionValues("app.test")).containsOnly("one");
4841
}
4942

5043
}
44+
// end::example[]

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

Lines changed: 5 additions & 5 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.
@@ -146,11 +146,11 @@ protected ConfigurableEnvironment getEnvironment() {
146146
}
147147

148148
/**
149-
* Get the {@link SpringBootTest#args()} (if present) specified in the annotated test
150-
* class. If no args given, returns empty array.
149+
* Return the application arguments to use. If no arguments are available, return an
150+
* empty array.
151151
* @param config the source context configuration
152-
* @return the {@link SpringBootTest#args()} (if present) specified in the annotated
153-
* test class, or empty array
152+
* @return the application arguments to use
153+
* @see SpringApplication#run(String...)
154154
*/
155155
protected String[] getArgs(MergedContextConfiguration config) {
156156
SpringBootTest annotation = AnnotatedElementUtils

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

Lines changed: 12 additions & 7 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.
@@ -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
@@ -106,12 +117,6 @@
106117
*/
107118
Class<?>[] classes() default {};
108119

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-
115120
/**
116121
* The type of web environment to create when applicable. Defaults to
117122
* {@link WebEnvironment#MOCK}.

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 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.
@@ -27,22 +27,24 @@
2727
import static org.assertj.core.api.Assertions.assertThat;
2828

2929
/**
30-
* Assert that tests annotated with {@link SpringBootTest} can specify
31-
* {@link SpringBootTest#args()} to be passed to their application under test.
30+
* Tests for {@link SpringBootTest} with application arguments.
3231
*
3332
* @author Justin Griffin
33+
* @author Stephane Nicoll
3434
*/
3535
@RunWith(SpringRunner.class)
36-
@SpringBootTest(args = { "--option.foo=option-foo-value", "other.bar=other-bar-value" })
36+
@SpringBootTest(args = { "--option.foo=foo-value", "other.bar=other-bar-value" })
3737
public class SpringBootTestArgsTests {
3838

3939
@Autowired
4040
private ApplicationArguments args;
4141

4242
@Test
4343
public void applicationArgumentsPopulated() {
44-
assertThat(this.args.getOptionNames()).contains("option.foo");
45-
assertThat(this.args.getNonOptionArgs()).contains("other.bar=other-bar-value");
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");
4648
}
4749

4850
@Configuration

0 commit comments

Comments
 (0)