junit4测试service-spring

1、配置方式:

import static org.junit.Assert.assertEquals;   

import org.junit.Before;   

import org.junit.Test;   

import org.springframework.context.ApplicationContext;   

import org.springframework.context.support.ClassPathXmlApplicationContext;   

  import com.job36.admanage.service.AdAdTypeService;   

import com.job36.admanage.vo.AdAdTypeVO;   

    public class AdTypeTest {   

    ApplicationContext dsContext = null;   

    AdAdTypeService adAdTypeService = null;   

    AdAdTypeVO adAdTypeVO = null;   

      @Before  

    public void init() throws Exception {   

        dsContext = new ClassPathXmlApplicationContext(new String[] {   

                "applicationContext.xml""applicationContext-dao.xml",   

                "applicationContext-service.xml" });   

                adAdTypeVO = new AdAdTypeVO();   

        adAdTypeVO.setTypeId(1);   

        adAdTypeVO.setTypeName("banner");   

        adAdTypeVO.setMessage("测试");   

        adAdTypeService = (AdAdTypeService) dsContext.getBean("AdAdTypeService");   

    }   

      @Test  

    public void findByCondition() {   

        assertEquals(adAdTypeService.findByCondition(adAdTypeVO).getListAdAdTypeVO().get(0), adAdTypeVO);   

    }   

}  

2、注解方式:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.funshion.base.ad.service.AdManager;
//使用@RunWith(SpringJUnit4ClassRunner.class),才能使测试运行于Spring测试//环境    
  @RunWith(SpringJUnit4ClassRunner.class)    
  //@ContextConfiguration 注解有以下两个常用的属性:    
   //locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件    
    //inheritLocations:是否要继承父测试类的 Spring 配置文件,默认为 true
//@ContextConfiguration(locations={"classpath*:/applicationContext.xml",
//   "classpath*:/applicationContext-activeMQ.xml"}) 
@ContextConfiguration(locations="classpath*:/applicationContext.xml") 
//如果只有一个配置文件就直接写locations=“配置文件路径+名”  
//多个文件也可以直接用正则表达式匹配,如"classpath:/context/applicationContext-*.xml" 
public class AdServiceTest {
 @Autowired
 private AdManager adManager ;

 public void test() {
  adManager.test();
 }
 @Test
 public void testAd(){
  System.out.println("ad----");
  adManager.test();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值