对于简单的静态方法,使用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());
}
}