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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM