当前位置: 代码迷 >> 综合 >> Exception: Caught exception while allowing TestExecutionListener
  详细解决方案

Exception: Caught exception while allowing TestExecutionListener

热度:2   发布时间:2024-03-09 15:14:12.0

前提

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 {@Autowiredprivate ICustomerTreeService customerTreeService;@Testpublic 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版本。

 

 

最后

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

 

  相关解决方案