当前位置: 首页 > 知识库问答 >
问题:

Spring boot 1.4测试:配置错误:发现@BootstrapBy的多个声明

洪浩波
2023-03-14

按照官方文档: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);
    }
}

如文件所述:

搜索算法从包含测试的包开始,直到找到@SpringBootApplication或@SpringBootConfiguration注释类。只要你以一种合理的方式构造了你的代码,你的主配置通常都会被找到。

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

授权服务:在com包下。xxx。yyy。zzz。批准

AuthorizationServiceTest:在com包下。xxx。yyy。zzz。授权测试;

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

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)

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

谢谢。

共有3个答案

于嘉誉
2023-03-14

我知道现在回答这个问题已经太晚了,但它可能会在将来帮助别人,所以。。。我也有同样的问题,经过一些研究,我发现如果有@SpringBootTest,就不应该有@WebMvcTest。所以只需删除@WebMvcTest@SpringBootTest就可以解决剩下的问题。

章岳
2023-03-14

只要删除@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 {

波姆。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>
楚俊杰
2023-03-14

当spring测试找不到主配置类时,会发生此异常。尝试将@ContextConfiguration anootation添加到测试类中。遵循spring测试文档了解更多细节(检测测试配置部分)

我的示例测试类如下:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
    ...
}
 类似资料:
  • 我正在对这些注释使用基本集成测试: 运行测试时,出现以下错误: 该错误是由以下注释引起的:SpringBootTest和DataMongoTest,包括BootstrapWith,如下所示: 我需要继续使用SpringBootTest进行SpringBootTest。WebEnvironment。RANDOM\u端口,但我还需要来自DataMongoTest的嵌入式mongodb 有什么建议吗?

  • 问题内容: 我想将所有错误定向到我的Errorsevlet,而无需明确指定所有代码。有什么办法可以这样做吗? 到达ErrorServlet后,如何获取servlet中错误的堆栈跟踪。这样一发生错误,我就可以通过电子邮件发送详细信息。 问题答案: 试试这个,所有错误都会被捕获(500个)而不是404等

  • 我正在与: Spring Framework JUnit 分级 我有这两个测试类 null Spring Boot/JUnit,为多个配置文件运行所有单元测试 但是我希望避免通过或使用命令,它通过类保持控件。

  • 你好 我正在Salesforce内部为trigger进行单元测试,我不断遇到一个似乎无法解决的错误,所以我希望有更多经验的人能帮助我回到正轨。我已经在Google上搜索过很多次了,我的代码结构也被弄乱了,但我找不到解决方案。 目的: 我的任务是编写一个触发器,该触发器将处理维护每个开发人员的案例排名所需的逻辑。每个开发人员都被分配了案例,这些案例可能有也可能没有由业务确定的优先级。每个开发人员在任

  • 在我的spring boot应用程序中,Logback正在将我抛到下面错误 我的登录配置是:

  • 我正在与之通信的服务器可以选择将多个调用加入到一个中。所以假设我加入2。。n个调用到一个调用中,响应可以检索0。。n个错误。有没有办法在一次一次性使用中避免多个错误?