Spring: A Developer's Notebook笔记和小结(9)
2006-01-13 21:36
344 查看
/**
作者:Willpower
来源:Rifoo Technology(http://www.rifoo.com)
时间:2006-01-13
备注:转载请保留以上声明
**/
我们前面已经实现了简单的视图,下面是我们做测试的时间了。Web MVC的易测试性是它的一大优点。我们接下来可以看到一段优雅的测试代码来测试用户接口的核心部分。
2.4.1. How do I do that?
在这个测试用例中,我们简单的去调用控制器,并且确定能够返回正确的模型给我们。首先,我们编写一个简单的JUnit test case来调用BikesController
Example 2-20. ControllerTest.java
public class ControllerTest extends TestCase {
private ApplicationContext ctx;
public void setUp( ) throws Exception {
//初始化上下文配置文件
ctx = new FileSystemXmlApplicationContext(
"war/WEB-INF/rentaBikeApp-servlet.xml");
}
public void testBikesController( ) throws Exception {
BikesController controller = (BikesController)
ctx.getBean("bikesController");
ModelAndView mav = controller.handleRequest(null, null);
RentABike store = (RentABike) mav.getModel( ).get("rentaBike");
assertNotNull(store);
assertTrue(store.getBikes( ).size( ) == 3);
}
}
我们必须加载配置文件来测试Spring正常的加载每个bean。这里我们用Spring提供的FileSystemXmlApplicationContext类来加载上下文配置。
接下来,我们要测试验证器,看它是否能够正确的验证错误。
Example 2-21. ControllerTest.java
public void testBikeValidator( ) throws Exception {
BikeValidator v = (BikeValidator) ctx.getBean("bikeValidator");
Bike bike = new Bike("test", "test", 1, "test", 2.00, "test");
Errors errs = new BindException(bike, "bike");
v.validate(bike, errs);
assertFalse(errs.hasErrors( ));
bike = new Bike( );
errs = new BindException(bike, "bike");
v.validate(bike, errs);
assertTrue(errs.hasErrors( ));
}
小结:我们现在已经完成了第二章的修改部分,这一章中我们学习了Spring MVC的简单用法,并且结合书中的代码进行了简单的补充。下一章中,我和大家一起继续学习Spring和其他用户接口框架的集成,如Struts和JSF的结合。
作者:Willpower
来源:Rifoo Technology(http://www.rifoo.com)
时间:2006-01-13
备注:转载请保留以上声明
**/
我们前面已经实现了简单的视图,下面是我们做测试的时间了。Web MVC的易测试性是它的一大优点。我们接下来可以看到一段优雅的测试代码来测试用户接口的核心部分。
2.4.1. How do I do that?
在这个测试用例中,我们简单的去调用控制器,并且确定能够返回正确的模型给我们。首先,我们编写一个简单的JUnit test case来调用BikesController
Example 2-20. ControllerTest.java
public class ControllerTest extends TestCase {
private ApplicationContext ctx;
public void setUp( ) throws Exception {
//初始化上下文配置文件
ctx = new FileSystemXmlApplicationContext(
"war/WEB-INF/rentaBikeApp-servlet.xml");
}
public void testBikesController( ) throws Exception {
BikesController controller = (BikesController)
ctx.getBean("bikesController");
ModelAndView mav = controller.handleRequest(null, null);
RentABike store = (RentABike) mav.getModel( ).get("rentaBike");
assertNotNull(store);
assertTrue(store.getBikes( ).size( ) == 3);
}
}
我们必须加载配置文件来测试Spring正常的加载每个bean。这里我们用Spring提供的FileSystemXmlApplicationContext类来加载上下文配置。
接下来,我们要测试验证器,看它是否能够正确的验证错误。
Example 2-21. ControllerTest.java
public void testBikeValidator( ) throws Exception {
BikeValidator v = (BikeValidator) ctx.getBean("bikeValidator");
Bike bike = new Bike("test", "test", 1, "test", 2.00, "test");
Errors errs = new BindException(bike, "bike");
v.validate(bike, errs);
assertFalse(errs.hasErrors( ));
bike = new Bike( );
errs = new BindException(bike, "bike");
v.validate(bike, errs);
assertTrue(errs.hasErrors( ));
}
小结:我们现在已经完成了第二章的修改部分,这一章中我们学习了Spring MVC的简单用法,并且结合书中的代码进行了简单的补充。下一章中,我和大家一起继续学习Spring和其他用户接口框架的集成,如Struts和JSF的结合。
相关文章推荐
- Spring: A Developer's Notebook笔记和小结(3)
- Spring: A Developer's Notebook笔记和小结(7)
- Spring: A Developer's Notebook笔记和小结(10)
- [索引帖]Spring: A Developer's Notebook笔记和小结(共21篇,结束)
- [原创]Spring: A Developer's Notebook笔记和小结(17)
- [原创]Spring: A Developer's Notebook笔记和小结(20)
- Spring: A Developer's Notebook笔记和小结(4)
- Spring: A Developer's Notebook笔记和小结(8)
- Spring: A Developer's Notebook笔记和小结(11)
- [原创]Spring: A Developer's Notebook笔记和小结(21)全部完
- Spring: A Developer's Notebook笔记和小结(12)
- Spring: A Developer's Notebook笔记和小结(13)
- [原创]Spring: A Developer's Notebook笔记和小结(14)
- Spring: A Developer's Notebook笔记和小结(1)
- Spring: A Developer's Notebook笔记和小结(5)
- [原创]Spring: A Developer's Notebook笔记和小结(15)
- [原创]Spring: A Developer's Notebook笔记和小结(18)
- Spring: A Developer's Notebook笔记和小结(2)
- Spring: A Developer's Notebook笔记和小结(6)
- [原创]Spring: A Developer's Notebook笔记和小结(16)