1. 程式人生 > >Spring Boot 項目在 IDEA 中 進行單元測試

Spring Boot 項目在 IDEA 中 進行單元測試

res sin run ng- targe clas 單元測試 2.0 就會

Spring Boot提供了許多實用程序和註釋來幫助您測試應用程序。

測試由兩個模塊提供支持:spring-boot-test包含核心項,spring-boot-test-autoconfigure支持測試的自動配置。

大多數開發人員使用spring-boot-starter-test,它會自動導入Spring Boot測試模塊以及JUnit,AssertJ,Hamcrest等許多其他有用的庫。

一般我們建立Spring Boot項目之後,pom.xml文件就會自帶spring-boot-starter-test依賴,如果沒有可以手動添加

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

然後我們在需要測試的類或接口上使用如下步驟建立測試類

  1) Ctrl + Shift + T

  2)Create New Test...

  3)Merber 選中需要測試的方法。

然後就會自動幫我們創建測試類,一般我們在類上加上下面兩個註解就可以了

@RunWith(SpringRunner.class)
@SpringBootTest

在方法上加上@Test註解就可以愉快的進行測試了

參考文檔:https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-testing

Spring Boot 項目在 IDEA 中 進行單元測試