JUnit4结合spring做单元测试

做单元测试非常简单,结合spring做单元测试也很简单。

首先,导包



先看一下目录结构:



下面是测试基类():

package pms_test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:/config/applicationContext.xml")
@Transactional
public class BaseSpringJunit {

	
	
}


实现类:

package pms_test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;

import cn.teacheredu.entity.UserEntity;
import cn.teacheredu.service.UserService;
import cn.teacheredu.service.batch.ProjectSummariesBatch;
import cn.teacheredu.utils.Md5Encrypt;

public class UserServiceTest extends BaseSpringJunit {

	private Integer TESTID = 1;

	@Autowired
	UserService userService;

	@Before
	public void init() {

	}

	@Rollback(false)
	public void insertUserEntity() {
		UserEntity userEntity = new UserEntity();
		userEntity.setLoginName("admin");
		userEntity.setPassword(Md5Encrypt.md5("123123"));
		userEntity.setRealname("张老三");
		userEntity.setDmId(0);
		userEntity.setRoleId(0);
		userEntity.setEmail("pms@teacheredu.cn");
		userEntity.setMobile("18141906787");
		userEntity.setIdcard("130132198903036754");
		userEntity.setGenTime(new Date());
		userEntity.setSex("0");
		int TESTID = 0;
		try {
			TESTID = userService.insertUserEntity(userEntity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("成功插入一条用户,id号:" + TESTID);
	}

	public void testGetUserEntityById() throws Exception {
		UserEntity userEntity = userService.getUserEntityById(TESTID);
		System.out.println(userEntity.toString());
		Assert.assertNotNull(userEntity);
	}

	@Autowired
	private ProjectSummariesBatch projectSummariesBatch;
	@Test
	@Rollback(false)
	public void testSummaryBatch() {
		try {
			Date dNow = new Date();   //当前时间
			Date dBefore = new Date();
			Calendar calendar = Calendar.getInstance(); //得到日历
			calendar.setTime(dNow);//把当前时间赋给日历
			calendar.add(Calendar.DAY_OF_MONTH, -365);  //设置为前一天
			dBefore = calendar.getTime();   //得到前一天的时间

			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
			String defaultStartDate = sdf.format(dBefore);   
			Date csDate = sdf.parse(defaultStartDate);
			/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse("2017-01-01");*/
			Date date = null;
//			projectSummariesBatch.summariesBatch(csDate);
			System.out.println("哈哈");
		} catch (Exception e) {
			System.out.println("尴尬了");
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值