【问题标题】:Spring Boot Test does not use jmx propertySpring Boot Test 不使用 jmx 属性
【发布时间】:2018-08-03 16:28:32
【问题描述】:

我有一个测试类

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class Foo{
    ...
}

它应该启动一个由以下定义的常规应用程序上下文:

@SpringBootApplication(scanBasePackages = {"de.foo", "de.bar"})
public class Application {
    ...
}

这按预期工作。此外,我有一个 application.yml,它在两种情况下都会被加载,但是在运行测试时,JMX (spring.jmx.enabled) 的属性没有被加载或没有被使用。

我尝试了不同的属性文件(application.yml、application-test.yml),但唯一有效的是通过设置属性

@TestPropertySource(properties = "spring.jmx.enabled:true")

该属性在常规应用程序上下文中默认为 true。

几个问题:

  1. 为什么测试类中的默认值不同?
  2. 为什么在从 application.yml 加载属性时无法加载或识别该属性(yml 的其余部分有效,所以它确实被加载了)。

这似乎是一种已知行为,如Spring Boot Sample Data Tests 中的此评论所示。是否有任何关于此行为的文档我错过了?

【问题讨论】:

    标签: java spring spring-boot jmx


    【解决方案1】:

    我自己最近也遇到了同样的情况,并打开了spring-projects/spring-boot#13008 来记录这种行为。因此,将在即将发布的1.5.13.RELEASE 和2.0.2.RELEASE 中添加参考手册的以下内容:

    由于测试上下文框架缓存上下文,JMX 默认是禁用的,以防止相同的组件在同一个域上注册。如果此类测试需要访问 MBeanServer,请考虑将其标记为脏:

    @RunWith(SpringRunner.class)
    @SpringBootTest(properties = "spring.jmx.enabled=true")
    @DirtiesContext
    public class SampleJmxTests {
    
        @Autowired
        private MBeanServer mBeanServer;
    
        @Test
        public void exampleTest() {
            // ...
        }
    
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    相关资源
    最近更新 更多