在 spring boot 2.1 中,由于多个 @BootstrapWith,不再允许许多测试切片 [英] in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith
问题描述
我尝试升级由两个测试切片(在我的例子中为 @JsonTest 和 @JdbcTest,中间有松脆的测试代码)制成的美味三明治,并为其添加了 spring boot 2.1 风味.但似乎并没有取得多大的成功.我无法用许多 @...Test 注释我的测试,因为它们现在每个都带有自己的 XxxTestContextBootstrapper.当他们都使用相同的 SpringBootTestContextBootstrapper 时,它曾经工作过.
I tried to upgrade a yummy sandwich made of two test slices (@JsonTest and @JdbcTest in my case, crunchy test code in between) adding spring boot 2.1 flavour to it. But it seems it was not much of a success. I cannot annotate my tests with many @...Test since they are now each bringing their own XxxTestContextBootstrapper. It used to work when they all used same SpringBootTestContextBootstrapper.
@RunWith(SpringRunner.class)
@JdbcTest
@JsonTest
public class Test {
@Test
public void test() { System.out.printn("Hello, World !"); }
}
我从 BootstrapUtils 得到的错误是 invalidStateException :配置错误:为测试类找到多个@BootstrapWith 声明
The error I get from BootstrapUtils is illegalStateException : Configuration error: found multiple declarations of @BootstrapWith for test class
我知道我可能在这里做错了什么,但是有没有一种简单的方法可以同时加载 Json 和 Jdbc 上下文?
I understand I might be doing something wrong here but is there an easy way I could load both Json and Jdbc contexts ?
推荐答案
测试切片注解并不是真正设计成这样组合的.恐怕你的代码在 Spring Boot 2.0 中运行是靠运气的.
Test slice annotations aren't really designed to be composed like that. Your code worked in Spring Boot 2.0 only by luck I'm afraid.
您确实只需要选择一个 @...Test
注释,然后将其与一个或多个 @AutoConfigure...
注释结合起来.对于上面的例子,我会写:
You really need to pick just one @...Test
annotation and then combine it with one or more @AutoConfigure...
annotations. For the example above, I would write:
@RunWith(SpringRunner.class)
@JdbcTest
@AutoConfigureJson
@AutoConfigureJsonTesters
public class Test {
@Test
public void test() {
System.out.println("Hello, World !");
}
}
这篇关于在 spring boot 2.1 中,由于多个 @BootstrapWith,不再允许许多测试切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!