Java Spring启动测试不使用jmx属性
我有一个考试班Java Spring启动测试不使用jmx属性,java,spring,spring-boot,jmx,Java,Spring,Spring Boot,Jmx,我有一个考试班 @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) public class Foo{ ... } 应启动以下定义的常规应用程序上下文: @SpringBootApplication(scanBasePackages = {"de.foo", "de.bar"}) public class Application { .
@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
几个问题:
为什么测试类中的默认值不同?
当从应用程序加载属性时,为什么没有加载或识别该属性。yml其余的yml都可以工作,所以它确实被加载了。
这似乎是一个已知的行为,如中的此评论所示。关于这种行为,我有没有遗漏任何文档?我最近也遇到过同样的情况,并且已经打开文档记录了这种行为。因此,在即将发布的1.5.13.1版和2.0.2.2版中,将对参考手册添加以下内容: 当测试上下文框架缓存上下文时,默认情况下禁用JMX,以防止相同的组件在同一域上注册。如果这样的测试需要访问一个MBEN服务器,请考虑将其标记为脏:
谢谢,我遇到了同样的问题。官方文件:
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.jmx.enabled=true")
@DirtiesContext
public class SampleJmxTests {
@Autowired
private MBeanServer mBeanServer;
@Test
public void exampleTest() {
// ...
}
}