Spring boot 1.4测试:配置错误:找到了@BootstrapWith的多个声明

les*_*nar 23 java spring spring-mvc spring-test spring-boot

按照官方文档:http: //docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing

我想测试一个我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如文件中所述:

搜索算法从包含测试的包开始工作,直到找到@SpringBootApplication或@SpringBootConfiguration注释类.只要您以合理的方式构建代码,通常就会找到主要配置.

我已正确构建了我的代码(至少我认为):

AuthorizationService:在包com.xxx.yyy.zzz.authorization下;

AuthorizationServiceTest:在包com.xxx.yyy.zzz.authorizationTest下;

我收到此异常(完整跟踪):

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)
Run Code Online (Sandbox Code Playgroud)

请帮帮我,我已经花了2-3个多小时没有运气.

谢谢.

小智 19

当spring test无法找到主配置类时,会发生此异常.尝试将@ContextConfiguration anootation添加到测试类中.有关详细信息,请参阅弹簧测试文档(检测测试配置部分)

我的示例Test类是这样的:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
    ...
}
Run Code Online (Sandbox Code Playgroud)


小智 16

只需删除@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 {
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)


小智 12

我知道现在回答这个问题为时已晚,但它可能会对将来的某人有所帮助,所以......我遇到了同样的问题,经过一些研究,我发现@WebMvcTest如果有@SpringBootTest. 所以只需删除@WebMvcTest@SpringBootTest处理其余部分。

  • 来自 [documentation](https://spring.io/guides/gs/testing-web/):`在这个测试中,完整的 Spring 应用程序上下文已启动,但没有服务器。我们可以通过使用 @WebMvcTest` 将测试范围缩小到仅 web 层,这意味着 `@WebMvcTest` 有助于通过不加载服务器和完整的应用程序上下文来使测试更轻松。因此,Nesrin 的回答更准确。 (2认同)

小智 5

发生这种情况是因为您同时声明了 @WebMvcTest 和 @SpringBootTest,我通过删除 @SpringBootTest 解决了我的相同问题