spring使用junit單元測試

 之前我們的測試類裏面都會有一個main方法,然後在通過ApplicationContext來獲取bean對象。那我們就要想辦法替換前面說的兩個點。

 

/**
 * 
 * Spring整合junit的配置
 *      1、pom.xml導入spring整合junit的jar(座標)
 *      2、使用Junit提供的一個註解把原有的main方法替換了,替換成spring提供的
 *             @Runwith
 *      3、告知spring的運行器,spring和ioc創建是基於xml還是註解的,並且說明位置
 *          @ContextConfiguration
 *                  locations:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下
 *                  classes:指定註解類所在地位置
 *
 *   當我們使用spring 5.x版本的時候,要求junit的jar必須是4.12及以上
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyBatisConfig.class)
public class Test {

    @Autowired
    private IAccountService accountService ;

    @org.junit.Test
    public void testMethod(){
        accountService.addAcount();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章