首先,测试类是测试功能的,作为开发人员,它能够保住你的money,但是测试类怎么写呢?
下面是一个简单的测试类,详情看注释:
package medicalplatform;
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 com.zhiyuan.bean.Charge;
import com.zhiyuan.dao.ChargeMapper;
//这句注解是让测试运行于spring测试环境
@RunWith(SpringJUnit4ClassRunner.class)
/**
* 【@ContextConfiguration 用来指定加载的Spring配置文件的位置,
* 会加载默认配置文件, 通常有两种属性:
* 1、locations可以通过手工指定spring配置文件所在位置
* 可以指定一个或多个 Spring 配置文件用,分开。
* 例如:locations={"aa/aa.xml","aa/ab.xml"}
* 2、inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。
*/
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class ChargeCeshi {
//自动装配,装配ChargeMapper接口
@Autowired
ChargeMapper chargemapper;
//Test注解申明为测试方法
@Test
public void TestMapper() {
//测试接口中的selectB呀PrimaryKey方法,
Charge res = chargemapper.selectByPrimaryKey(1);
//这里的getPatientid方法是实体类的方法,输出查到了的patientid数据。
System.out.println(res.getPatientid());
}
}
那么还要强调一点,这个测试类的运行是运行JUNIT,而且是直接运行。
插入一个额外的话题,main()方法也是直接运行,那么是先运行main()方法还是先运行测试类呢?
答案:用java虚拟机运行才是java程序中main()方法的运行方式,而测试类是要JUNIT运行才行。可能说的不够清楚请原谅。