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
61f16fe6
Commit
61f16fe6
authored
Oct 20, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.5.x'
parents
a8e0b0dd
c88559e0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
5 deletions
+66
-5
spring-boot-features.adoc
...ing-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+66
-5
No files found.
spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
61f16fe6
...
@@ -5651,11 +5651,12 @@ The search algorithm works up from the package that contains the test until it f
...
@@ -5651,11 +5651,12 @@ The search algorithm works up from the package that contains the test until it f
<<using-boot-structuring-your-code, structured your code>> in a sensible way your main
<<using-boot-structuring-your-code, structured your code>> in a sensible way your main
configuration is usually found.
configuration is usually found.
NOTE: If you use a <<boot-features-testing-spring-boot-applications-testing-autoconfigured-tests,
NOTE: If you use a
test annotation to test a more specific slice of your application>> with such setup, any
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-tests, test
user configuration defined on your `@SpringBootApplication` will be processed. This can
annotation to test a more specific slice of your application>> with such setup, you should
break the purpose of slicing and it is recommended to move such configuration to a
avoid adding configuration that are specific to a particular area on the
`@Configuration` class alongside your `@SpringBootApplication`.
<<boot-features-testing-spring-boot-applications-testing-user-configuration, main's
application class>>.
If you want to customize the primary configuration, you can use a nested
If you want to customize the primary configuration, you can use a nested
`@TestConfiguration` class. Unlike a nested `@Configuration` class which would be used
`@TestConfiguration` class. Unlike a nested `@Configuration` class which would be used
...
@@ -6528,6 +6529,66 @@ include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigu
...
@@ -6528,6 +6529,66 @@ include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigu
[[boot-features-testing-spring-boot-applications-testing-user-configuration]]
==== User configuration and slicing
If you've <<using-boot-structuring-your-code, structured your code>> in a sensible way,
your `@SpringBootApplication` class is
<<boot-features-testing-spring-boot-applications-detecting-config, used by default>> as
the configuration of your tests.
It then becomes important not to litter the application's main class with configuration
that are are specific to a particular area of its functionality.
Let's assume that you are using Spring Batch and you're relying on the auto-configuration
for it. Your could define your `@SpringBootApplication` as follows:
[source,java,indent=0]
----
@SpringBootApplication
@EnableBatchProcessing
public class SampleApplication { ... }
----
Because this class is the source configuration for the test, any slice test will actually
attempt to start Spring Batch, which is definitely now what you want to do. A recommended
approach is to move that area-specific configuration to a separate `@Configuration`
class at the same level as your application.
[source,java,indent=0]
----
@Configuration
@EnableBatchProcessing
public class BatchConfiguration { ... }
----
NOTE: Depending on the surface area of your application, you may either have a single
`ApplicationConfiguration` class for your customizations or one class per domain area
when it makes sense. The latter approach allows you to enable it in one of your test
if necessary via `@Import`.
Another source of confusion is classpath scanning. Let's assume that, while you've
structured your code in a sensible way, you need to scan an additional package. Your
application may look like this:
[source,java,indent=0]
----
@SpringBootApplication
@ComponentScan({ "com.example.app", "org.acme.another" })
public class SampleApplication { ... }
----
This effectively overrides the default component scan directive with the side effect of
scanning those two packages regardless of the slice that you've chosen. For instance a
`@DataJpaTest` will all the sudden scan components and user configurations of your
application. Again, moving the custom directive to a separate class is a good way to fix
this issue.
TIP: If this is not an option for you, you can create a `@SpringBootConfiguration`
somewhere in the hierarchy of your test so that it is used instead. Or you can specify
a source for your test which will disable the behaviour of finding a default one.
[[boot-features-testing-spring-boot-applications-with-spock]]
[[boot-features-testing-spring-boot-applications-with-spock]]
==== Using Spock to test Spring Boot applications
==== Using Spock to test Spring Boot applications
If you wish to use Spock to test a Spring Boot application you should add a dependency
If you wish to use Spock to test a Spring Boot application you should add a dependency
...
...
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