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

时间:2016-12-09 09:54:26

标签: java spring spring-mvc spring-boot spring-test

按照官方文件: 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   注释类。只要你在合理的情况下构建代码   通常可以找到您的主要配置。

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

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)

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

感谢。

8 个答案:

答案 0 :(得分:17)

当spring test无法找到主配置类时,会发生此异常。 尝试将@ContextConfiguration anootation添加到测试类中。请按照弹簧测试文档获取更多详细信息(第Detecting test configuration部分)

我的示例Test类是这样的:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
    ...
}

答案 1 :(得分:13)

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

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
   </parent>

答案 2 :(得分:4)

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

答案 3 :(得分:3)

我知道回答这个问题为时已晚,但是将来可能会对某人有所帮助。。。我遇到了同样的问题,经过一番研究,我发现如果有@WebMvcTest就不应该了是@SpringBootTest。因此只需删除@WebMvcTest@SpringBootTest就会处理其余的工作。

答案 4 :(得分:0)

希望这会有所帮助。将其从@SpringBootTest删除后,它对我有用,后来在测试Web是否具有相同的@AutoWired时会抛出错误,因为我使用了界面。与其模拟它的工作原理。

 package com.naveen.productreview;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.naveen.productreview.service.IProductReviewService;

@RunWith(SpringRunner.class)
@WebMvcTest
//@SpringBootTest
public class WebLayerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IProductReviewService productReviewService; 

    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.mockMvc.perform(get("/api/product-review")).andDo(print()).andExpect(status().isOk())
        .andExpect(content().string(containsString("Naveen"))); 
    }
}

答案 5 :(得分:0)

好像您要编写集成测试。这就是为什么我建议仅使用

@SpringBootTest(classes = Application.class)

如果要使用junit4,请添加

@RunWith(SpringRunner.class)

答案 6 :(得分:0)

在Junit 5中可能是:

@ExtendWith(SpringExtension.class)
@DataJpaTest
class DeliveryRepositoryTest {

    @Autowired
    private DeliveryRepository repository;
    ...
}

答案 7 :(得分:0)

我在这个设置中遇到了类似的问题:

@WebMvcTest
@SpringBootTest
@ContextConfiguration(classes = {Controller.class})
public class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

...}

并且我在控制器中自动装配了一个 Spring 服务 bean。

最后的正确设置是只留下提供 MockMvc 的 @WebMvcTest,并为服务添加一个 @MockBean。 我最后的工作测试是:

@WebMvcTest(Controller.class)
public class ControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CustomerService customerService; 

...}