Skip to content

Commit b3ad902

Browse files
committed
Merge branch '1.5.x' into 2.0.x
2 parents 1fb51dc + 830e523 commit b3ad902

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6096,7 +6096,13 @@ context by default only if you use `SpringApplication` to create it.
60966096
Spring Boot provides a `@SpringBootTest` annotation, which can be used as an alternative
60976097
to the standard `spring-test` `@ContextConfiguration` annotation when you need Spring
60986098
Boot features. The annotation works by creating the `ApplicationContext` used in your
6099-
tests through `SpringApplication`.
6099+
tests through `SpringApplication`. In addition to `@SpringBootTest` a number of other
6100+
annotations are also provided for
6101+
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-tests,testing more
6102+
specific slices>> of an application.
6103+
6104+
TIP: Don't forget to also add `@RunWith(SpringRunner.class)` to your test, otherwise
6105+
the annotations will be ignored.
61006106

61016107
You can use the `webEnvironment` attribute of `@SpringBootTest` to further refine how
61026108
your tests run:
@@ -6121,13 +6127,6 @@ test method by default. However, as using this arrangement with either `RANDOM_P
61216127
run in separate threads and, thus, in separate transactions. Any transaction initiated on
61226128
the server does not roll back in this case.
61236129

6124-
NOTE: In addition to `@SpringBootTest`, a number of other annotations are also provided
6125-
for testing more specific slices of an application. You can find more detail throughout
6126-
this chapter.
6127-
6128-
TIP: Do not forget to add `@RunWith(SpringRunner.class)` to your test. Otherwise, the
6129-
annotations are ignored.
6130-
61316130

61326131

61336132
[[boot-features-testing-spring-boot-applications-detecting-web-app-type]]
@@ -6245,6 +6244,19 @@ include::{code-examples}/test/web/RandomPortTestRestTemplateExampleTests.java[ta
62456244

62466245

62476246

6247+
[[boot-features-testing-spring-boot-applications-jmx]]
6248+
==== Using JMX
6249+
As the test context framework caches context, JMX is disabled by default to prevent
6250+
identical components to register on the same domain. If such test needs access to an
6251+
`MBeanServer`, consider marking it dirty as well:
6252+
6253+
[source,java,indent=0]
6254+
----
6255+
include::{test-examples}/jmx/SampleJmxTests.java[tag=test]
6256+
----
6257+
6258+
6259+
62486260
[[boot-features-testing-spring-boot-applications-mocking-beans]]
62496261
==== Mocking and Spying Beans
62506262
When running tests, it is sometimes necessary to mock certain components within your
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.jmx;
18+
19+
import org.springframework.boot.SpringBootConfiguration;
20+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
22+
23+
/**
24+
* A sample {@link SpringBootConfiguration} that only enables JMX auto-configuration.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
@SpringBootConfiguration
29+
@ImportAutoConfiguration(JmxAutoConfiguration.class)
30+
public class SampleApp {
31+
32+
}
Lines changed: 50 additions & 0 deletions
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.jmx;
18+
19+
import javax.management.MBeanServer;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.test.annotation.DirtiesContext;
27+
import org.springframework.test.context.junit4.SpringRunner;
28+
29+
/**
30+
* Example integration test that uses JMX.
31+
*
32+
* @author Stephane Nicoll
33+
*/
34+
// tag::test[]
35+
@RunWith(SpringRunner.class)
36+
@SpringBootTest(properties = "spring.jmx.enabled=true")
37+
@DirtiesContext
38+
public class SampleJmxTests {
39+
40+
@Autowired
41+
private MBeanServer mBeanServer;
42+
43+
@Test
44+
public void exampleTest() {
45+
// ...
46+
47+
}
48+
49+
}
50+
// end::test[]

0 commit comments

Comments
 (0)