一、導入jar
<!--Junit3預設程式設計的方式,junit4注解-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
二、建立Test檔案夾
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcdXWU5kMr1mYshmMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DMxYDMyEDN2ETNyATM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
java右鍵,Mark Directory as ----->Test Source Root
三、建立測試類
1.在需要測試的類中 Ctrl+Shift+T,建立測試類,别忘記選擇JUnit4
2.在測試類前,配置需要的配置檔案(我此處隻測試dao層,是以需要告訴Junit spring-dao檔案所在位置,為了junit啟動時加載springIOC容器),如果不需要可以不配置
@RunWith(SpringJUnit4ClassRunner.class)
//告訴junit spring 配置檔案
@ContextConfiguration({"classpath:config/spring-dao.xml"})
3.編寫測試方法,運作即可