SSM添加junit

在框架中添加单元测试是必要的,以junit为例,我们来看一下junit的添加步骤。

1、在root中添加单元测试的依赖:



junit
junit
4.12
test


2、在servicepom中添加引用:



junit
junit


3、3、添加测试的Util类,来加载所有的spring文件,启动容器:

Public class CommonApi{

publicBeanFactoryfactory;
publicItemServiceitemService;

@Before
publicvoidsetUp(){
factory=newClassPathXmlApplicationContext("classpath:spring/applicationContext*.xml");
itemService=(ItemService)factory.getBean("itemServiceImpl");
}
}


4、新建测试类,继承Util类,内容如下:

4、新建测试类,继承Util类,内容如下:

Public class ItemTest extends CommonApi{

@Test
publicvoidgetItemById(){
longitemId=738388;
TbItemtbitem=itemService.getItemById(itemId);
System.out.println(tbitem);
}
}


至此,添加完成,文件目录如下:

SSM添加junit_第1张图片


你可能感兴趣的:(●,项目篇,SSM框架搭建整合)