SSH中如何进行单元测试

来源:互联网 发布:凯翔公棚最新数据 编辑:程序博客网 时间:2024/05/06 23:18

相信不少朋友都遇见过类似问题,现在介绍一个JUNIT4的测试方法。

首先在已经集成好SSH的环境中

添加

junit4.jar 

   spring-test.jar

接着在项目中新建一个source folder,名为test

一般测试类的起名规范为

XXXTest


[java] view plaincopy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml")  
  3. public class TalkTest extends AbstractJUnit4SpringContextTests {  
  4.     @Resource  
  5.     TalkDao talkDaoImpl ;  
  6.     @Resource  
  7.     StudentDao studentDaoImpl;  
  8.       
  9.     @Test  
  10.     public void getOneTalk(){  
  11.         Talk talk =  talkDaoImpl.getById(1);  
  12.         System.out.println(talk.getStudent().getName());  
  13.     }   
  14.       
  15.   
  16.     @Test  
  17.     public void getOneStudent(){  
  18.         Student student =  studentDaoImpl.getById(1);  
  19.             Iterator<Talk> ite  =student.getTalks().iterator();  
  20.             while(ite.hasNext()){  
  21.                 System.out.println(ite.next().getContent());  
  22.             }  
  23.     }   
  24.     @Test  
  25.     public void getAllTalk(){  
  26.         List<Talk> talks  = talkDaoImpl.findTalkToPage(010"");  
  27.         System.out.println(talks.get(0).getStudent().getName());      
  28.     }  
  29. }  
测试类继承AbstractJUnit4SpringContextTests

如果需要事务支持的话  需要继承另外一个类这里不再赘述


然后添加 



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")

这两个注解  都比较简单

然后在需要测试的方法上面添加@Test注解就好了

右键方法名 Run As

是不是很方便?

0 0
原创粉丝点击