测试类怎么写?

首先,测试类是测试功能的,作为开发人员,它能够保住你的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运行才行。可能说的不够清楚请原谅。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值