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