Spring Boot 集成JUnit5进行测试 Error creating bean with name 'serverEndpointExporter' defined in class path resource

1、一开始加入了一个方法,测试只要是选择了JUnit5,都不走。也不报错,也不成功。

解决:pom.xml中加入以下的jar包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!-- 以下是后期加入的,加入就可以了--> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency>

2、测试的时候,简单的输出一个字符串,一切正常,但是@AutoWired进行注解的对象,直接就是null

记住,测试的包与aplication的包要是同一个,如果是com.xxx.aa 那么测试的也应该是com.xx.aa,一个在src/main/java下,一个是src/test/java下。

解决:

@RunWith(SpringRunner.class)  
@SpringBootTest

3、然后测试报Error creating bean with name 'serverEndpointExporter' defined in class path resource

解决:

这是因为代码中引用了websocket,这个里面有一个@Bean导致,其实应该是让其在tomcat中执行。

@RunWith(SpringRunner.class)  
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

再次运行,一切正常,可以顺利测试了。

参考:https://blog.csdn.net/qq_27101653/article/details/85072241

道法自然
原文地址:https://www.cnblogs.com/jiduoduo/p/14902967.html