相信不少朋友都遇见过类似问题,现在介绍一个JUNIT4的测试方法。
首先在已经集成好SSH的环境中
添加
junit4.jar
spring-test.jar
接着在项目中新建一个source folder,名为test
一般测试类的起名规范为
XXXTest
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:applicationContext.xml")
- public class TalkTest extends AbstractJUnit4SpringContextTests {
- @Resource
- TalkDao talkDaoImpl ;
- @Resource
- StudentDao studentDaoImpl;
- @Test
- public void getOneTalk(){
- Talk talk = talkDaoImpl.getById(1);
- System.out.println(talk.getStudent().getName());
- }
- @Test
- public void getOneStudent(){
- Student student = studentDaoImpl.getById(1);
- Iterator<Talk> ite =student.getTalks().iterator();
- while(ite.hasNext()){
- System.out.println(ite.next().getContent());
- }
- }
- @Test
- public void getAllTalk(){
- List<Talk> talks = talkDaoImpl.findTalkToPage(0, 10, "");
- System.out.println(talks.get(0).getStudent().getName());
- }
- }
如果需要事务支持的话 需要继承另外一个类这里不再赘述
然后添加
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
这两个注解 都比较简单
然后在需要测试的方法上面添加@Test注解就好了
右键方法名 Run As
是不是很方便?