Junit進行測試ssh項目

對於簡單的靜態方法,使用public static void main就可以測試了,當時想調用service的方法時,就需要連接數據庫,牽扯到事務。簡單的測試main方法已經不能滿足需求了,所以要需要使用Junit編寫單元測試。

公司一個比較老的ssh的項目。resources下配置文件如下
在這裏插入圖片描述
config.properties 爲數據庫的地址,用戶名,密碼等…
spring.xml 是對於config.properties的引用。

綜上所述,測試類的完整源碼如下:

package sy.test.temp;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sy.model.base.Sywxuser;
import sy.service.base.SywxuserServiceI;

import java.util.List;

/**
 * @author yearns
 * @date 2019/6/5 10:25
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:spring.xml", "classpath:spring-hibernate.xml"})
public class JunitTest {
    @Autowired
    private SywxuserServiceI sywxuserServiceI;
    @Test
    public void getWxuser(){
        String hql="from Sywxuser t where 1=1";
        List<Sywxuser> sywxusers = sywxuserServiceI.find(hql);
        System.out.println(sywxusers.size());
    }
}

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