基于Spring环境构建单元测试

提纲:

一、背景

二、示例

三、延伸

一、背景

代码可测性,代码单元测试,TDD,,,,等无一不要求能够快速构建测试环境。那么在Spring环境下如何快速加载相应的配置,构建测试环境呢?本章就举一个小例子来说明

二、示例

不打算说太多废话,直接上代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class SendFaxBizTest {

    @Autowired
    private FaxSendFacade faxFacadeImpl;

    @Test
    public void testHtmlSend1() {
        try {
            logger.info("action....");
            faxFacadeImpl.faxSend(,,,)...#业务代码删除
        } catch (Exception e) {
            logger.warn("error", e);
        }
    }

}

   其实全部的秘密都在下面的两行Annotation里面

   @RunWith(SpringJUnit4ClassRunner.class)

   @ContextConfiguration(locations = { "classpath:applicationContext.xml" })

三、延伸

  思考?Spring如何与Juint结合?@RunWith与 @ContextConfiguration(locations的内幕是怎样?

  有待补充,暂时没时间去看

猜你喜欢

转载自kongxuan.iteye.com/blog/2021436