关于Spring+的测试

  使用了Spring+的产品,默认需要使用集成测试了。Spring通过Spring TestContext Framework对集成测试提供顶级支持,其不依赖于特定的测试框架。下面示例使用了Spring+中不同产品的测试:

  1、使用了Spring,参考Spring配置之常用配置概述中关于Profile的部分。对于测试部分具体说明如下:

                

    1)、SpringJUnit4ClassRunner在JUnit环境下提供Sprint TestContext Framework的功能。

    2)、ContextConfiguration用来加载配置ApplicationContext,其中classes属性用来加载配置类。

    3)、@ActiveProfiles用来声明活动的profile;

  2、使用了SpringMVC:

    web项目如果启动失败就不能直接进行测试了,启动成功后才能进行测试(如果一直失败解决不了,是不是就直接影响了测试的进度了)。为了测试Web项目不需要启动项目需要一些Servlet相关的模拟对象,比如MockMVC、MockHttpServletRequest等。示例如下:

    1)控制类:

                

    2)服务类:

                

    3)测试类:

                

                

    4)pom.xml:

                

    5)测试结果:

                

 

posted on 2024-03-25 20:06  池塘里洗澡的鸭子  阅读(3)  评论(0编辑  收藏  举报