junit3测试spring下的service层
2008-06-25 16:57
357 查看
如果你已经整合好了hibernate和spring,那么恭喜你。如果你需要测试业务层,那么或许能给你帮助。
在已经写好的service右击,选择new---> Junit test case, 选1.3,选setup(),选一个要测试的方法。
例如
public class HotelService {
/**
* @param args
*/
private CountryDAO countryDao;
public List getCountryList()
{
List list=new ArrayList();
countryDao.findById(1);
System.out.println("记录");
return list;
}
public CountryDAO getCountryDao() {
return countryDao;
}
public void setCountryDao(CountryDAO countryDao) {
this.countryDao = countryDao;
}
}
生成的
public class HotelServiceTest extends TestCase
{
private Country country;
//以下是取得ctx 的两种方式。绝对路径和相对路径。
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"classpath*:applicationContext.xml");
/*
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"file:D:/Eclipse3.2/SpringTest2/WebRoot/WEB-INF/classes/applicationContext.xml");
*/
CountryDAO countryDao = (CountryDAO) ctx.getBean("CountryDAO");
protected void setUp() throws Exception
{
super.setUp();
}
public void testGetCountryList()
{
country=countryDao.findById(1);
System.out.println(country.getCtEname());
}
}
运行此测试的时候没问题。有问题请留言。
在已经写好的service右击,选择new---> Junit test case, 选1.3,选setup(),选一个要测试的方法。
例如
public class HotelService {
/**
* @param args
*/
private CountryDAO countryDao;
public List getCountryList()
{
List list=new ArrayList();
countryDao.findById(1);
System.out.println("记录");
return list;
}
public CountryDAO getCountryDao() {
return countryDao;
}
public void setCountryDao(CountryDAO countryDao) {
this.countryDao = countryDao;
}
}
生成的
public class HotelServiceTest extends TestCase
{
private Country country;
//以下是取得ctx 的两种方式。绝对路径和相对路径。
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"classpath*:applicationContext.xml");
/*
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"file:D:/Eclipse3.2/SpringTest2/WebRoot/WEB-INF/classes/applicationContext.xml");
*/
CountryDAO countryDao = (CountryDAO) ctx.getBean("CountryDAO");
protected void setUp() throws Exception
{
super.setUp();
}
public void testGetCountryList()
{
country=countryDao.findById(1);
System.out.println(country.getCtEname());
}
}
运行此测试的时候没问题。有问题请留言。
相关文章推荐
- junit3测试spring下的service层
- JavaEE_Junit_Junit对Spring注入的Service层的测试
- spring + junit 测试
- spring junit完美测试解决方案
- springboot集成junit测试与javamail测试遇到的问题
- SpringMVC+Spring+hibernate+junit的Dao测试
- 用Spring提供的JUnit框架扩展对DAO或Service层进行单元测试
- Junit使用教程--整合spring测试套件
- Junit+spring创建JNDI运行测试用例
- spring - 使用 IDEA编译工具: Junit测试
- spring dbunit junit hibernate 注解方式测试集合
- SpringBoot中Junit测试注入Bean失败的解决方法
- Spring+EasyMock对Service层方法进行测试,处理@Resource属性
- spring AOP 配置事务不起作用的问题(用Junit测试service)
- Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试
- spring junit 部署两套测试方案
- Spring整合JUnit实现测试用例
- junit测试和spring整合
- spring框架学习(三)spring与junit整合测试