Skip to content

Commit 830e523

Browse files
committed
Clarify behavior of JMX in @SpringBootTest
Closes spring-projectsgh-13008
1 parent dd3f57d commit 830e523

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5363,7 +5363,13 @@ features of Spring Boot are only installed in the context by default if you use
53635363
Spring Boot provides a `@SpringBootTest` annotation which can be used as an
53645364
alternative to the standard `spring-test` `@ContextConfiguration` annotation when you need
53655365
Spring Boot features. The annotation works by creating the `ApplicationContext` used
5366-
in your tests via `SpringApplication`.
5366+
in your tests via `SpringApplication`. In addition to `@SpringBootTest` a number of other
5367+
annotations are also provided for
5368+
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-tests,testing more
5369+
specific slices>> of an application.
5370+
5371+
TIP: Don't forget to also add `@RunWith(SpringRunner.class)` to your test, otherwise
5372+
the annotations will be ignored.
53675373

53685374
You can use the `webEnvironment` attribute of `@SpringBootTest` to further refine
53695375
how your tests will run:
@@ -5388,12 +5394,6 @@ or `DEFINED_PORT` implicitly provides a real servlet environment, HTTP client an
53885394
server will run in separate threads, thus separate transactions. Any transaction
53895395
initiated on the server won't rollback in this case.
53905396

5391-
NOTE: In addition to `@SpringBootTest` a number of other annotations are also
5392-
provided for testing more specific slices of an application. See below for details.
5393-
5394-
TIP: Don't forget to also add `@RunWith(SpringRunner.class)` to your test, otherwise
5395-
the annotations will be ignored.
5396-
53975397

53985398

53995399
[[boot-features-testing-spring-boot-applications-detecting-config]]
@@ -5482,6 +5482,19 @@ include::{code-examples}/test/web/RandomPortExampleTests.java[tag=test-random-po
54825482

54835483

54845484

5485+
[[boot-features-testing-spring-boot-applications-jmx]]
5486+
==== Using JMX
5487+
As the test context framework caches context, JMX is disabled by default to prevent
5488+
identical components to register on the same domain. If such test needs access to an
5489+
`MBeanServer`, consider marking it dirty as well:
5490+
5491+
[source,java,indent=0]
5492+
----
5493+
include::{test-examples}/jmx/SampleJmxTests.java[tag=test]
5494+
----
5495+
5496+
5497+
54855498
[[boot-features-testing-spring-boot-applications-mocking-beans]]
54865499
==== Mocking and spying beans
54875500
It's sometimes necessary to mock certain components within your application context when
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.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.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)