2012-05-31 71 views
0

我正在使用Powermock 1.4.9和Easymock擴展。EasyMock PowerMock IllegalStateException使用Ant而不是IDE

以下是,我想測試

Public class SomeClass { 
    public String methodToTest(String id) throws Exception { 
     try { 
      ClassA ojbectA = MyFactory.staticMethod(); 
      String str = helperMethod(id); 
      ojbectA.methodA(str); 
      return objectA.methodB(str); 
     } 
     catch (Exception e) { 
      throw e; 
     } 
    } 
} 

以下是測試類和我所寫入測試上述方法測試方法。我使用PowerMock和EasyMock,因爲我有Static和Private方法調用。

import org.easymock.EasyMock; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.powermock.api.easymock.PowerMock; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

import static org.easymock.EasyMock.isA; 
import static org.powermock.api.easymock.PowerMock.mockStatic; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest{{SomeClass.class,MyFactory.class, ClassA.class}} 
public class TestClass { 
@Test 
public void testMethod1() { 
    try { 
     ClassA mockA = PowerMock.createMock(ClassA.class); 
     mockStatic(MyFactory.class); 
     SomeClass someObj = PowerMock.createPartialMock(SomeClass.class,"helperMethod"); 

     String mockLog = "This is sample"; 
     String mockId = "mockId"; 
     String woMockId = "WO_mockId"; 

     //Mock all static methods 
     EasyMock.expect(MyFactory.staticMethod()).andReturn(mockA); 

     //Partial mocking 
     PowerMock.expectPrivate(someObj,"helperMethod",mockId).andReturn(woMockId); 

     //Mock methodA(String) method 
     mockA.methodA(EasyMock.isA(String.class)); 
     PowerMock.expectLastCall().times(0, 1); 

     //When methodB(String) is called, intercept it and return a standard value. 
     EasyMock.expect(mockA.methodB(isA(String.class))).andReturn(mockLog); 

     EasyMock.replay(mockA); 
     EasyMock.replay(someObj); 
     PowerMock.replay(MyFactory.class); 

     String obtainedResult = someObj.getWorkorderLogByWoId(mockId); 

     assertEquals(obtainedResult,mockLog); 


     EasyMock.verify(mockA); 
     EasyMock.verify(someObj); 
     PowerMock.verify(MyFactory.class); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

這對Intellij IDEA和Eclipse都沒有任何異常,運行良好。但是,當我使用ANT在命令行運行此測試時,我收到以下異常。

java.lang.IllegalStateException: no last call on a mock available 
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521) 
at org.easymock.EasyMock.expect(EasyMock.java:499) 
at com.xyz.ems.server.services.workorder.TestClass.testMethod1(TestClass.java:<some_line_number>) 
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:597) 
at junit.framework.TestCase.runTest(TestCase.java:168) 
at junit.framework.TestCase.runBare(TestCase.java:134) 
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:124) 
at junit.framework.TestSuite.runTest(TestSuite.java:243) 
at junit.framework.TestSuite.run(TestSuite.java:238) 
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518) 
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052) 
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906) 

有原始代碼的請求而不替換字符串。所以下面是代碼。

import com.xyz.ems.common.UnitTestCase; 
import com.xyz.ems.server.services.logmessage.LogMessageManager; 
import com.xyz.ems.server.services.workorder.database.WorkOrderManager; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

import static org.easymock.EasyMock.expect; 
import static org.easymock.EasyMock.isA; 
import static org.powermock.api.easymock.PowerMock.createMock; 
import static org.powermock.api.easymock.PowerMock.createPartialMock; 
import static org.powermock.api.easymock.PowerMock.expectLastCall; 
import static org.powermock.api.easymock.PowerMock.expectPrivate; 
import static org.powermock.api.easymock.PowerMock.mockStatic; 
import static org.powermock.api.easymock.PowerMock.replay; 
import static org.powermock.api.easymock.PowerMock.verify; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({WorkOrderManager.class,WorkOrderFactory.class,  LogMessageManager.class}) 
public class WorkOrderManagerPM_JTest extends UnitTestCase { 


public WorkOrderManagerPM_JTest(String name) { 
    super(name); 
} 

@Before 
public void firstMethod() { 
    assertTrue(true); 
} 

@After 
public void lastMethod() { 
    assertTrue(true); 
} 

@Test 
public void testGetWoLogByObjectId() { 
    try { 
     LogMessageManager logMsgMgrMock = createMock(LogMessageManager.class); 
     mockStatic(WorkOrderFactory.class); 
     WorkOrderManager wMgr = createPartialMock(WorkOrderManager.class,"buildWorkOrderIdString"); 

     String mockLog = "This is sample"; 
     String mockId = "mockId"; 
     String woMockId = "WO_mockId"; 

     //Mock all static methods 
     expect(WorkOrderFactory.getLogMessageManager()).andReturn(logMsgMgrMock); 

     //Partial mocking 
     expectPrivate(wMgr,"buildWorkOrderIdString",mockId).andReturn(woMockId); 

     //Mock openLogMessageDoc() method 
     logMsgMgrMock.openLogMessageDoc(isA(String.class)); 
     expectLastCall().times(0, 1); 

     //When getAllLogMessageAsString() is called, intercept it and return a standard value. 
     expect(logMsgMgrMock.getAllLogMessagesAsString(isA(String.class))).andReturn(mockLog); 

     replay(logMsgMgrMock); 
     replay(wMgr); 
     replay(WorkOrderFactory.class); 

     String obtainedResult = wMgr.getWorkorderLogByWoId(mockId); 

     assertEquals("getWorkorderLogByWoId() general case passed",obtainedResult,mockLog); 


     verify(logMsgMgrMock); 
     verify(wMgr); 
     verify(WorkOrderFactory.class); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     fail("Failed to test getWorkorderLogById()"); 
    } 
} 

和被測原始的方法...

public class WorkOrderManager { 
public String getWorkorderLogByWoId(String woId) throws WorkOrderServiceEMSException { 
    try { 
     LogMessageManager logMsgMgr = WorkOrderFactory.getLogMessageManager(); 
     String workOrderLogId = buildWorkOrderIdString(woId); 
     logMsgMgr.openLogMessageDoc(workOrderLogId); 
     return logMsgMgr.getAllLogMessagesAsString(workOrderLogId); 
    } 
    catch (Exception e) { 
     String msg = "Failed to get Work Order Log for workorder Id : " + woId; 
     throw new WorkOrderServiceEMSException(msg, WorkOrderServiceEMSError.WO_GET_WOLOG_BY_OBJECTID.newInstanceWithFormatArgs(woId), e); 
    } 
} 
} 
+0

發表您的進口 – Thihara

+0

更新我的代碼片段import語句。謝謝。 – maverick

+0

用原代碼更新了我的代碼片段 – maverick

回答

0

試試這個....

package com.chumma.pdf.main; 

import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Mock; 
import org.mockito.Mockito; 
import org.powermock.api.mockito.PowerMockito; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ SomeClass.class, MyFactory.class, ClassA.class }) 
public class SomeClassTest { 
    SomeClass someClass; 

    @Mock 
    ClassA mockClassA; 

    @Before 
    public void inti() { 
     someClass = PowerMockito.spy(new SomeClass()); 
    } 

    @Test 
    public void test() throws Exception { 
     // To mock MyFactory.staticMethod(); 
     PowerMockito.mockStatic(MyFactory.class); 
     Mockito.when(MyFactory.staticMethod()).thenReturn(mockClassA); 
     // To mock helperMethod(id); 
     PowerMockito.when(someClass, "helperMethod", Mockito.anyString()) 
       .thenReturn("retrunString"); 
     // To mock ojbectA.methodA(str); 
     Mockito.when(mockClassA.methodA(Mockito.anyString())).thenReturn(
       "retStr1"); 
     // To mock ojbectA.methodB(str); 
     Mockito.when(mockClassA.methodB(Mockito.anyString())).thenReturn(
       "retStr2"); 
    } 
} 
+0

我正在使用powermock-easymock-1.4.9-full.jar,它是帶有easymock擴展的powermock。從你的進口報表中我看到你用mockito擴展使用powermock。我只能用easymock estension來使用powermock。我的壞,我應該提前提到。最令我困擾的是它可以與IDE協同工作,但不適用於ANT。任何線索爲什麼會發生這種情況? 爲了好奇,我會嘗試你的解決方案,看看它是否有效。 – maverick

+0

如果你可以發佈實際的源代碼,這可能會有所幫助....它看起來像你已經將測試類的名稱改爲TestClass等等。如果你可以給實際的類和日誌,這將有很大的幫助... – om39a

+0

@ om39a:我發佈了實際的代碼。讓我知道如果有什麼不明確的。 – maverick

相關問題