Exception: Caught exception while allowing TestExecutionListener

前提

SpringBoot版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
</parent>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
</dependency>

问题

SpringBoot集成测试测试用例时,启动报错抛出异常。

09:05:21.042 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@7fcf2fc1] to prepare test instance [ICustomerTreeServiceTest@6bbe2511]
java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V
	

代码

@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class ICustomerTreeServiceTest {

    @Autowired
    private ICustomerTreeService customerTreeService;


    @Test
    public void testGetTreeData() {
        final CustomerTreeParam tree = new CustomerTreeParam();
        tree.setId("00556068331");
        final List<Map> treeData = customerTreeService.getTreeData(tree);
    }
}

解决方案

一般情况下新创建的SpringBoot项目中集成SpringBoot测试用例是没问题的,但是由于我接手的是别人构建的项目,在项目内部引用了其他的公共项目依赖,而在这个依赖中还有Spring Cloud的依赖,由于SpringBoot和Spring Cloud版本不兼容,导致项目启动报错,测试用例也跑不起来。

1.因为我们不需要Spring Cloud项目所以就把依赖排除了。

<dependency>
   <artifactId>product-center-provider</artifactId>
   <groupId>center</groupId>
   <version>4.0-SNAPSHOT</version>
   <exclusions>
        <exclusion>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.选择兼容的Spring Cloud版本。

 

 

最后

希望这篇博客能帮到有缘人,大家一起加油吧。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值