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();
}
}