Eclipse中使用junit测试Service层

        从05年以来一直使用struts+spring+Hibernate进行开发,但是对Serveice层的测试从来也没有认真地使用过junit,今天周末在家试了一下,其实挺简单的。

       项目的源代码放在src中,再建立一个名为test的Source folder,专么放测试类,直接在要测试的Service上面右单击 ,如下图:

新建一个JUnit Test Case 文件,如下图:

Source floder 改到test文件夹,选中setUp()。

         编写测试代码:

package  net.mengfanpp.service;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.FileSystemXmlApplicationContext;

import  junit.framework.TestCase;
import  net.mengfanpp.domain.Product;

public   class  IProductServiceTest  extends  TestCase  {

    
//WebContent/WEB-INF/spring/ 是我的spring配置文件存放目录
    ApplicationContext ctx = new FileSystemXmlApplicationContext(
            
"WebContent/WEB-INF/spring/*.xml");

    IProductService productService 
= (IProductService) ctx
            .getBean(
"productService");

    
/*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#setUp()
     
*/

    
protected void setUp() throws Exception {
        
super.setUp();
    }


    
/**
     * Test method for
     * {
@link net.mengfanpp.service.IProductService#getProductByID(int)}.
     
*/

    
public void testGetProductByID() {
        
// fail("Not yet implemented");
        System.out.println("testing...");
        Product product 
= productService.getProductByID(1);
        System.out.println(product.toString());

    }


}

然后,在该文件上右键单击“Run As ——〉Junit Test”查看测试结果!

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值