話不多說,直入正題。
添加依賴
<!--SpringJUnit4ClassRunner測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
測試類中進行測試
@SpringBootTest(classes = {CmfzdemoApplication.class}) //必寫 大括號中的值爲啓動類的名稱
@RunWith(SpringJUnit4ClassRunner.class) //必寫
public class AppTest {
@Autowired
private ArticleService articleService;
@Autowired
private AlbumService albumService;
@Test
public void Test1(){
System.out.println(articleService.getArticle());
}
@Test
public void Test2(){
System.out.println(albumService.getAlbum());
}
}
1.添加所需依賴
2.測試類上添加 SpringBootTest RunWith 註解
3.SpringBootTest 註解中大括號的值填寫啓動類的名稱,RunWith 註解照抄
4.方法名上添加Test註解
上述四部完成後,即可進行方便快捷的springjunit測試。
這輩子堅持與不堅持都不可怕,怕的是獨自走在堅持的道路上!
歡迎加入技術羣聊!