读书人

在Spring框架上 使用junit进行单元测试

发布时间: 2012-10-09 10:21:45 作者: rapoo

在Spring框架下 使用junit进行单元测试

package com.yourPackage.test;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yourPackage.service.ServiceA;
import com.yourPackage.service.ServiceB;

public class MyTest {
??? private PnlCachingService caching;
??? private PnlHistoricalService hist;
??? @Before
??? public void setUp() throws Exception {
??? ??? ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"file:WebContent/WEB-INF/applicationContext.xml"});
??? ??? BeanFactory factory = (BeanFactory) context;
??? ??? serviceA = (ServiceA) factory.getBean("ServiceA");
??? ??? serviceB = (ServiceB) factory.getBean("ServiceB");
??? }

??? @Test
??? public void testYourService(){
??? ???
??? ??? try{
??? ??? ??? List rtnA = serviceA.functionA();
??? ??? ??? List rtnB = serviceB.functionB();
??? ??? ??? Assert.assertEquals(rtnA, rtnB);
??? ??? }catch (Exception e){
??? ??? ??? e.printStackTrace();
??? ??? }
??? }
}

读书人网 >软件架构设计

热点推荐