![](/img/trans.png)
[英]Spring boot 1.4 Testing : Configuration error: found multiple declarations of @BootstrapWith
[英]Spring boot 2.2.0 Testing : Configuration error: found multiple declarations of @BootstrapWith
@RunWith(SpringRunner.class)
@SpringBootTest(classes=MyApplication.class)
@TestPropertySource(locations = "classpath:test-application.properties")
@WebAppConfiguration
@RestClientTest(Controller.class)
public class MyIntegrationTest {
}
当我运行它时,我收到以下错误 java.lang.IllegalStateException: 配置错误:发现多个 @BootstrapWith 声明用于测试 class
看起来您的测试中至少有一个太多的 Spring 测试注释。
你到底想测试什么? 如果它确实只是一个RestClientTest
,那么这应该可以工作:
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:test-application.properties")
@RestClientTest(YourRestClient.class)
public class MyIntegrationTest {
}
或者, go 只是@SpringBootTest
,但我不知道你想在这里做什么。
根本原因是您同时使用@SpringBootTest和@RestClientTest 。
因为它会导致关于@BootstrapWith注释的冲突。 你可以看到下面的图片。
提示:如果您使用的是 JUnit 4,请不要忘记将 @RunWith(SpringRunner.class) 也添加到您的测试中,否则注释将被忽略。 如果您使用的是 JUnit 5,则无需将等效的 @ExtendWith(SpringExtension.class) 添加为 @SpringBootTest 并且其他 @…Test 注释已经用它进行了注释。
参考: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing
如果您的测试用例很简单,您可以在此处参考使用@RestClientTest的示例。
在我的例子中,我定义了一些与测试服务相关的复杂配置(例如:jpaAuditingHandler、jpaMappingContext、...)。 这就是我使用@SpringBootTest 进行自动配置的原因。
这是我的代码示例:
@SpringBootTest
class MyTest {
@Autowired
private MyProperties myProperties;
@Autowired
private MyService myService;
private MockRestServiceServer server;
@Autowired
@Qualifier("restTemplateBean1")
private RestTemplate restTemplate;
@BeforeEach
void setUp() {
server = MockRestServiceServer.createServer(restTemplate);
}
@Test
void testCallRestServiceSuccess() throws Exception {
this.server.expect(requestTo(myProperties.getUrl())).andRespond(withStatus(HttpStatus.OK));
boolean result = myService.callRestService();
assertThat(result).isTrue();
}
}
其他一些参考资料:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.