论坛首页 入门技术论坛

使用EasyMock 对service层的测试的问题

浏览 2184 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-07-12  
使用EasyMock 对service层的测试

serviceImpl的代码

public class MedicineServiceImpl  implements
        MedicineService {

protected PersistenceIF persistence;

public Collection<DrugstoreStorage> createMedicine(Medicine medicine) {

        persistence.create(medicine);
        .....业务逻辑

        persistence.saveOrUpdateAll(mappingList);
        persistence.saveOrUpdateAll(saveStoregeList);
        return saveStoregeList;
    }

测试代码

public class MedicineServiceImplTest extends TestCase {
    private MedicineServiceImpl medicineService;

    private PersistenceIF persistenceMock;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
         medicineService = new MedicineServiceImpl();
         persistenceMock = EasyMock.createMock(PersistenceIF.class);
         medicineService.setPersistence(persistenceMock);
    }
    @Test
    public void testCreateMedicine(){
        Medicine medicine = new Medicine();
        medicine.setId(new Long(1));
        medicine.setName("medicine1");
        MedicineClass mClass = new MedicineClass();
        mClass.setId(new Long(1));
        mClass.setName("class1");
        medicine.setMedicineClass(mClass);

        Collection<Hospital> hospitals = new ArrayList<Hospital>();
        //hospital
        Hospital hospital = new Hospital();
        hospital.setId(new Long(1));
        hospital.setName("hospital1");
        hospital.setBank("bank11");
        Set<Drugstore> drugStoreList = new HashSet<Drugstore>();
        Drugstore drugStore1 = new Drugstore();
        drugStore1.setId(new Long(1));
        drugStore1.setHospital(hospital);
        drugStore1.setName("store1");
       
        Drugstore drugStore2 = new Drugstore();
        drugStore2.setId(new Long(1));
        drugStore2.setHospital(hospital);
        drugStore2.setName("store2");
       
        drugStoreList.add(drugStore1);
        drugStoreList.add(drugStore2);
       
        hospital.setStorages(drugStoreList);
        hospitals.add(hospital);
       
        List<MedicineCodeMapping> mappingList = new ArrayList<MedicineCodeMapping>();
        MedicineCodeMapping mapping = new MedicineCodeMapping();
        mapping.setHospital(hospital);
        mapping.setMedicine(medicine);
        mappingList.add(mapping);
       
        Collection<DrugstoreStorage> drugStorageListA = new ArrayList<DrugstoreStorage>();
        DrugstoreStorage storage1 = new DrugstoreStorage();

        storage1.setMapping(mapping);
        storage1.setDrugstore(drugStore1);
       
        DrugstoreStorage storage2 = new DrugstoreStorage();
        storage2.setMapping(mapping);
        storage2.setDrugstore(drugStore1);
       
        drugStorageListA.add(storage1);
        drugStorageListA.add(storage2);
       
        Filter filter = new EntityFilter(Hospital.class);
        persistenceMock.create(medicine);             
        expect(persistenceMock.query(filter)).andReturn(hospitals);
        persistenceMock.saveOrUpdateAll(mappingList);
        persistenceMock.saveOrUpdateAll(drugStorageListA);
        replay(persistenceMock);
        Collection<DrugstoreStorage> drugStorageListB = medicineService.createMedicine(medicine);
        verify(persistenceMock);
        assertEquals(drugStorageListA, drugStorageListB);
    }
}

dao

public interface PersistenceIF {

    Serializable create(Object entity);
        void saveOrUpdateAll(final Collection entities);
}


测试代码在persistenceMock.create(medicine);     这里会报错
java.lang.AssertionError:
  Unexpected method call create([Medicine] ID = 1, CREATE_TIME = Thu Jul 12 15:11:44 CST 2007, MODIFY_TIME = Thu Jul 12 15:11:44 CST 2007, IS_DELETED = false, CREATOR = admin, MODIFIER = null, ):
    query(deleted=false): expected: 1, actual: 0
    saveOrUpdateAll([[MedicineCodeMapping] ID = -1, CREATE_TIME = Thu Jul 12 15:11:44 CST 2007, MODIFY_TIME = Thu Jul 12 15:11:44 CST 2007, IS_DELETED = false, CREATOR = admin, MODIFIER = null, ]): expected: 1, actual: 0
    saveOrUpdateAll([[DrugstoreStorage] ID = -1, CREATE_TIME = Thu Jul 12 15:11:44 CST 2007, MODIFY_TIME = Thu Jul 12 15:11:44 CST 2007, IS_DELETED = false, CREATOR = admin, MODIFIER = null, , [DrugstoreStorage] ID = -1, CREATE_TIME = Thu Jul 12 15:11:44 CST 2007, MODIFY_TIME = Thu Jul 12 15:11:44 CST 2007, IS_DELETED = false, CREATOR = admin, MODIFIER = null, ]): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:29)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:45)
    at $Proxy0.create(Unknown Source)
    at com.infowarelab.pharmasky.service.impl.CommonServiceImpl.save(CommonServiceImpl.java:52)
    at com.infowarelab.pharmasky.service.medicine.impl.MedicineServiceImpl.createMedicine(MedicineServiceImpl.java:471)
    at com.infowarelab.pharmasky.service.medicine.impl.MedicineServiceImplTest.testCreateMedicine(MedicineServiceImplTest.java:152)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:164)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:228)
    at junit.framework.TestSuite.run(TestSuite.java:223)
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


   发表时间:2007-07-13  
有人吗。。。。
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics