传统方法,在类里写main方法,调用需要测试的方法。
单元测试:是单一实体(类或方法)的测试。
约定:
类放在test包中
类名用XXXTest结尾
方法用testMethod命名
@Test:
注解在测试方法上边,
(expected=XXException.class) 用来抛出异常,有异常测试也通过。
(timeout=xxx)用来限制测试时间,超时测试失败。
failures:是指测试失败
error:是测试程序本身出错
@Rollback(false)
在测试方法上面加上这句,测试数据不回滚
Eclipse新建测试类:
项目new>junit test case (新建测试实例)。
名字,参考约定。
class under test 选择要测试的类,下一步选择要测试的方法。
其他注释:
@Ignore: 被忽略的测试方法
@Before: 每一个测试方法之前运行
@After: 每一个测试方法之后运行
@BeforeClass: 所有测试开始之前运行(例如链接数据库,加载配置文件之类)
@AfterClass: 所有测试结束之后运行(关闭资源)
测试多个类或者方法:
run as 选择run configuration选择要测试的方法。
具体案例测试Controller:
package *****;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.annotation.Before;
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 org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import **.entity.***;
import **.service.***;
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@WebAppConfiguration
@ContextConfiguration(
locations = {
"classpath:spring-mvc.xml" ,
"classpath:spring-hibernate.xml" ,
"classpath:spring.xml"
})
@Transactional
public class DryeyeControllerTest {
@Autowired
private ****Service ***Service;
@Test
public void test方法名() {
***Service.方法名(1, 3));
}
}
测试里的具体情况需要根据具体测试的类的情况而定。
@RunWith(SpringJUnit4ClassRunner.class),
让测试运行于Spring测试环境。以便在测试开始的时候自动创建Spring的应用上下文。
加载配置文件:
@ContextConfiguration(
locations = {
"classpath:spring-mvc.xml" ,
"classpath:spring-hibernate.xml" ,
"classpath:spring.xml"
})
测试Service:
package work.dao.impl;
import static org.junit.Assert.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
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 org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4进行测试
@ContextConfiguration(locations = { "classpath:spring-mvc.xml", "classpath:spring-hibernate.xml",
"classpath:spring.xml" })
@Transactional
public class DryeyeDaoImplTest {
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession() {
return this.sessionFactory.getCurrentSession();
}
@Test
public void testGetTotal() {
String Hql = "select count(*) from Dryeye";
Query query = this.getCurrentSession().createQuery(Hql);
int total = ((Long) query.iterate().next()).intValue();
// int total = ((Long) query.iterate().next()).intValue();
System.out.println(total);
}
}
测试方法和Contorller一样。需要加载配置文件。