Java Spring boot 1.4 测试:配置错误:发现@BootstrapWith 的多个声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41057602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot 1.4 Testing : Configuration error: found multiple declarations of @BootstrapWith
提问by lesnar
By following the official doc here: http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing
按照这里的官方文档:http: //docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing
i wanted to test one of my REST API method like this:
我想像这样测试我的 REST API 方法之一:
@RunWith(SpringRunner.class)
@WebMvcTest(LoginController.class)
@SpringBootTest(classes = Application.class)
public class AuthorizationServiceTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void test() {
Object returnedObject=his.restTemplate.getForObject("/login", Object.class);
}
}
As stated in the doc :
正如文档中所述:
The search algorithm works up from the package that contains the test until it finds a @SpringBootApplication or @SpringBootConfiguration annotated class. As long as you've structure your code in a sensible way your main configuration is usually found.
搜索算法从包含测试的包开始工作,直到找到 @SpringBootApplication 或 @SpringBootConfiguration 注释类。只要您以合理的方式构建代码,通常就会找到您的主要配置。
I have structured my code properly(atleast i think ):
我已经正确构建了我的代码(至少我认为):
AuthorizationService: is under package com.xxx.yyy.zzz.authorization;
AuthorizationService: 在包 com.xxx.yyy.zzz.authorization 下;
AuthorizationServiceTest: is under package com.xxx.yyy.zzz.authorizationTest;
AuthorizationServiceTest: 在包 com.xxx.yyy.zzz.authorizationTest 下;
I am getting this exception(Full Trace):
我收到此异常(完整跟踪):
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.orangeraid.rasberry.gateway.authorizationTest.AuthorizationServiceTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:155)
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:126)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Please help me with this, i have already spent more than 2-3 hours without any luck.
请帮我解决这个问题,我已经花了超过 2-3 个小时没有任何运气。
Thanks.
谢谢。
回答by mahkras
This exception occurs when spring test can not find main configuration class. Try to add @ContextConfiguration anootation to your test class. Follow the spring test documention for more details (section Detecting test configuration)
当 spring test 找不到主配置类时会发生此异常。尝试将 @ContextConfiguration anootation 添加到您的测试类。有关更多详细信息,请参阅 spring 测试文档(检测测试配置部分)
My example Test class is like this:
我的示例测试类是这样的:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
...
}
回答by Nesrin
Just Remove the @SpringBootTest and everything work fine. I had the same problem with using @SpringBootTest and @DataJpaTest, this error raised when I upgraded the pom.xml parent springboot version to 2.1.0 as below. When I was using version 2.0.5, this error was not raising.
只需删除@SpringBootTest,一切正常。我在使用 @SpringBootTest 和 @DataJpaTest 时遇到了同样的问题,当我将 pom.xml 父 springboot 版本升级到 2.1.0 时出现了这个错误,如下所示。当我使用 2.0.5 版时,没有出现此错误。
@RunWith(SpringRunner.class)
//@SpringBootTest//(classes = KalahApplication.class)
// DataJpaTest supports rollback after running every test case
@DataJpaTest
public class GameRepositoryTest {
pom.xml
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
回答by jalil
I know its too late to answer this question, but it may help someone in the future so... I had the same issue and after some research, i found that there shouldn't be @WebMvcTest
if there is @SpringBootTest
. so just remove @WebMvcTest
and @SpringBootTest
is taking care of the rest.
我知道现在回答这个问题为时已晚,但它可能会在将来对某人有所帮助,所以......我遇到了同样的问题,经过一些研究,我发现@WebMvcTest
如果有@SpringBootTest
. 所以只需删除@WebMvcTest
并@SpringBootTest
处理其余部分。
回答by iman bhlool
It happens because you declared both @WebMvcTest and @SpringBootTest, I resolve my same problem by removing @SpringBootTest
发生这种情况是因为您同时声明了 @WebMvcTest 和 @SpringBootTest,我通过删除 @SpringBootTest 解决了我的相同问题