Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7413584b
Commit
7413584b
authored
Feb 13, 2019
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Support expressing application `args` in `@SpringBootTest`"
Closes gh-14823
parent
422e6b7d
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
36 deletions
+39
-36
spring-boot-features.adoc
...ing-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+7
-5
ApplicationArgumentsExampleTests.java
...t/docs/test/context/ApplicationArgumentsExampleTests.java
+7
-13
SpringBootContextLoader.java
...gframework/boot/test/context/SpringBootContextLoader.java
+5
-5
SpringBootTest.java
...org/springframework/boot/test/context/SpringBootTest.java
+12
-7
SpringBootTestArgsTests.java
...gframework/boot/test/context/SpringBootTestArgsTests.java
+8
-6
No files found.
spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
7413584b
...
@@ -6752,18 +6752,20 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
...
@@ -6752,18 +6752,20 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
`@SpringBootApplication`) you need to register the `TypeExcludeFilter` with it. See
`@SpringBootApplication`) you need to register the `TypeExcludeFilter` with it. See
{dc-spring-boot}/context/TypeExcludeFilter.{dc-ext}[the Javadoc] for details.
{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
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]
[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]]
[[boot-features-testing-spring-boot-applications-testing-with-mock-environment]]
==== Testing with a mock environment
==== Testing with a mock environment
By default, `@SpringBootTest` does not start the server. If you have web endpoints that
By default, `@SpringBootTest` does not start the server. If you have web endpoints that
...
...
spring-boot-project/spring-boot-docs/src/
test/java/org/springframework/boot/docs
/context/ApplicationArgumentsExampleTests.java
→
spring-boot-project/spring-boot-docs/src/
main/java/org/springframework/boot/docs/test
/context/ApplicationArgumentsExampleTests.java
View file @
7413584b
/*
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -14,22 +14,21 @@
...
@@ -14,22 +14,21 @@
* limitations under the License.
* 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.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
org.springframework.test.context.junit4.SpringRunner
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
// tag::example[]
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
args
=
{
"--foo=bar"
}
)
@SpringBootTest
(
args
=
"--app.test=one"
)
public
class
ApplicationArgumentsExampleTests
{
public
class
ApplicationArgumentsExampleTests
{
@Autowired
@Autowired
...
@@ -37,14 +36,9 @@ public class ApplicationArgumentsExampleTests {
...
@@ -37,14 +36,9 @@ public class ApplicationArgumentsExampleTests {
@Test
@Test
public
void
applicationArgumentsPopulated
()
{
public
void
applicationArgumentsPopulated
()
{
assertThat
(
this
.
args
.
getOptionNames
()).
contains
(
"foo"
);
assertThat
(
this
.
args
.
getOptionNames
()).
containsOnly
(
"app.test"
);
assertThat
(
this
.
args
.
getOptionValues
(
"foo"
)).
contains
(
"bar"
);
assertThat
(
this
.
args
.
getOptionValues
(
"app.test"
)).
containsOnly
(
"one"
);
}
// end::args[]
@Configuration
protected
static
class
Config
{
}
}
}
}
// end::example[]
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java
View file @
7413584b
/*
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -146,11 +146,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
...
@@ -146,11 +146,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
}
}
/**
/**
*
Get the {@link SpringBootTest#args()} (if present) specified in the annotated test
*
Return the application arguments to use. If no arguments are available, return an
*
class. If no args given, returns
empty array.
* empty array.
* @param config the source context configuration
* @param config the source context configuration
* @return the
{@link SpringBootTest#args()} (if present) specified in the annotated
* @return the
application arguments to use
*
test class, or empty array
*
@see SpringApplication#run(String...)
*/
*/
protected
String
[]
getArgs
(
MergedContextConfiguration
config
)
{
protected
String
[]
getArgs
(
MergedContextConfiguration
config
)
{
SpringBootTest
annotation
=
AnnotatedElementUtils
SpringBootTest
annotation
=
AnnotatedElementUtils
...
...
spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java
View file @
7413584b
/*
/*
* Copyright 2012-201
8
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -25,6 +25,7 @@ import java.lang.annotation.Target;
...
@@ -25,6 +25,7 @@ import java.lang.annotation.Target;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.WebApplicationType
;
import
org.springframework.boot.WebApplicationType
;
...
@@ -55,6 +56,8 @@ import org.springframework.web.context.WebApplicationContext;
...
@@ -55,6 +56,8 @@ import org.springframework.web.context.WebApplicationContext;
* specified.</li>
* specified.</li>
* <li>Allows custom {@link Environment} properties to be defined using the
* <li>Allows custom {@link Environment} properties to be defined using the
* {@link #properties() properties attribute}.</li>
* {@link #properties() properties attribute}.</li>
* <li>Allows application arguments to be defined using the {@link #args() args
* attribute}.</li>
* <li>Provides support for different {@link #webEnvironment() webEnvironment} modes,
* <li>Provides support for different {@link #webEnvironment() webEnvironment} modes,
* including the ability to start a fully running web server listening on a
* including the ability to start a fully running web server listening on a
* {@link WebEnvironment#DEFINED_PORT defined} or {@link WebEnvironment#RANDOM_PORT
* {@link WebEnvironment#DEFINED_PORT defined} or {@link WebEnvironment#RANDOM_PORT
...
@@ -93,6 +96,14 @@ public @interface SpringBootTest {
...
@@ -93,6 +96,14 @@ public @interface SpringBootTest {
@AliasFor
(
"value"
)
@AliasFor
(
"value"
)
String
[]
properties
()
default
{};
String
[]
properties
()
default
{};
/**
* Application arguments that should be passed to the application under test.
* @return the application arguments to pass to the application under test.
* @see ApplicationArguments
* @see SpringApplication#run(String...)
*/
String
[]
args
()
default
{};
/**
/**
* The <em>annotated classes</em> to use for loading an
* The <em>annotated classes</em> to use for loading an
* {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also
* {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also
...
@@ -106,12 +117,6 @@ public @interface SpringBootTest {
...
@@ -106,12 +117,6 @@ public @interface SpringBootTest {
*/
*/
Class
<?>[]
classes
()
default
{};
Class
<?>[]
classes
()
default
{};
/**
* Arguments that should be passed to the application under test.
* @return the arguments to pass to the application under test.
*/
String
[]
args
()
default
{};
/**
/**
* The type of web environment to create when applicable. Defaults to
* The type of web environment to create when applicable. Defaults to
* {@link WebEnvironment#MOCK}.
* {@link WebEnvironment#MOCK}.
...
...
spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestArgsTests.java
View file @
7413584b
/*
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -27,13 +27,13 @@ import org.springframework.test.context.junit4.SpringRunner;
...
@@ -27,13 +27,13 @@ import org.springframework.test.context.junit4.SpringRunner;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
/**
* Assert that tests annotated with {@link SpringBootTest} can specify
* Tests for {@link SpringBootTest} with application arguments.
* {@link SpringBootTest#args()} to be passed to their application under test.
*
*
* @author Justin Griffin
* @author Justin Griffin
* @author Stephane Nicoll
*/
*/
@RunWith
(
SpringRunner
.
class
)
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
args
=
{
"--option.foo=
option-
foo-value"
,
"other.bar=other-bar-value"
})
@SpringBootTest
(
args
=
{
"--option.foo=foo-value"
,
"other.bar=other-bar-value"
})
public
class
SpringBootTestArgsTests
{
public
class
SpringBootTestArgsTests
{
@Autowired
@Autowired
...
@@ -41,8 +41,10 @@ public class SpringBootTestArgsTests {
...
@@ -41,8 +41,10 @@ public class SpringBootTestArgsTests {
@Test
@Test
public
void
applicationArgumentsPopulated
()
{
public
void
applicationArgumentsPopulated
()
{
assertThat
(
this
.
args
.
getOptionNames
()).
contains
(
"option.foo"
);
assertThat
(
this
.
args
.
getOptionNames
()).
containsOnly
(
"option.foo"
);
assertThat
(
this
.
args
.
getNonOptionArgs
()).
contains
(
"other.bar=other-bar-value"
);
assertThat
(
this
.
args
.
getOptionValues
(
"option.foo"
)).
containsOnly
(
"foo-value"
);
assertThat
(
this
.
args
.
getNonOptionArgs
())
.
containsOnly
(
"other.bar=other-bar-value"
);
}
}
@Configuration
@Configuration
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment