2017-08-31 578 views
1

我試圖測試拋出的異常,但代碼繼續返回true而不是捕獲異常並返回false。PowerMockito doThrow不拋出異常

測試類

@RunWith(PowerMockRunner.class) 
@PrepareForTest({Class1.class, Class2.class}) 
public class TestClass 
{ 

private Argument arg; 
private static Class1 mockObj1; 
private static Class2 mockObj2; 

@BeforeClass 
public static void initialSetup() 
{ 
    PowerMockito.mockStatic(Class1.class); 
    mockObj1 = PowerMockito.mock(Class1.class); 
    mockObj2 = PowerMockito.mock(Class2.class); 
} 

@Before 
public void setupForEachTest() 
{ 
    arg = new Argument(); 
} 

@Test 
public void testUpdate() throws RemoteException 
{ 
    PowerMockito.when(Class1.getDefault()).thenReturn(mockObj1); 
    PowerMockito.when(mockObj1.getClass2()).thenReturn(mockObj2); 
    PowerMockito.doThrow(new RemoteException()).when(mockObj2).save(arg); 
    assertFalse(rule.update(null, null, null, null, null, null, null, null, null, null, null)); 
} 

被測試方法

public boolean update(params) 
{ 
    try 
    {   
     Class1.getDefault().getClass2().save(args); 
    } 

    catch(RemoteException e) 
    { 
     Log.error(this, e); 
     return false; 
    } 

    return true; 
} 

我每次得到一個斷言錯誤我嘗試運行此我試圖同時做拋和thenThrow方法任何幫助將不勝感激。

編輯:

堆棧跟蹤

java.lang.AssertionError: 
    at org.junit.Assert.fail(Assert.java:91) 
    at org.junit.Assert.assertTrue(Assert.java:43) 
    at org.junit.Assert.assertFalse(Assert.java:68) 
    at org.junit.Assert.assertFalse(Assert.java:79) 
    at package.TestClass.testUpdate(TestClass.java:92) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326) 
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86) 
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298) 
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84) 
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134) 
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) 
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) 
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136) 
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121) 
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57) 
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

保存方法

public abstract int save(Argument paramarg) 
throws RemoteException; 
+0

您是否嘗試過添加'catch(異常e) { Log.error(this,e); 返回false; }'?也許這是一些其他類型的例外? –

+0

你可以更新你的問題,包括(a)顯示「斷言錯誤」的堆棧跟蹤和(b)Class2.save()的實現。 – glytching

+0

我已編輯帖子以包含堆棧跟蹤 – sharkzooka

回答

0

我還是有點問題的混淆。具體做法是:

  • 測試實例arg = new Argument()@Before方法這意味着arg是相關的每個測試。
  • 提供的update()方法聲明如下所示:public boolean update(params),它沒有定義params參數的類型。
  • 問題也提供了save()方法此實現:int save(Argument paramarg) throws RemoteException;這意味着Argument arg傳遞到update()因此通過擴展params類型必須爲Argument,但測試用例調用rule.update(null, null, null, ...)

Anwyay,根據這些假設:

  • 更新()的方法是:

    public boolean update(Argument params) { 
        try { 
         Class1.getDefault().getClass2().save(params); 
        } catch (RemoteException e) { 
         return false; 
        } 
    
        return true; 
    } 
    
  • save()方法是;

    save(Argument arg) throws RemoteException 
    

下測試通過:

@RunWith(PowerMockRunner.class) 
@PrepareForTest({Class1.class}) 
public class ATestClass { 

    private Argument arg; 
    private Class1 mockObj1; 
    private Class2 mockObj2; 

    @Before 
    public void initialSetup() { 
     PowerMockito.mockStatic(Class1.class); 

     mockObj1 = Mockito.mock(Class1.class); 

     mockObj2 = Mockito.mock(Class2.class); 
    } 

    @Before 
    public void setupForEachTest() { 
     arg = new Argument(); 
    } 

    @Test 
    public void testUpdate() throws RemoteException { 
     PowerMockito.when(Class1.getDefault()).thenReturn(mockObj1); 
     Mockito.when(mockObj1.getClass2()).thenReturn(mockObj2); 

     Mockito.when(mockObj2.save(Mockito.any(Argument.class))).thenThrow(new RemoteException()); 

     Assert.assertFalse(update(arg)); 
    } 
} 

注意:您只需要使用PowerMock藉此調用的護理:Class1.getDefault(),所有其他的嘲笑調用既不是靜態的也不是最後這樣的Mockito就足夠了。

+0

這工作完全謝謝你,我仍然是新的堆棧溢出,Java和junit測試一般,所以我試圖格式化問題盡我所能。你的假設是非常正確的,只有我把它作爲類中的局部變量。抱歉的混淆。 – sharkzooka

+0

@sharkzooka以供將來參考,您可以按照[這些指南](https://stackoverflow.com/help/mcve)創建一個最小的,可驗證的完整示例。另一種考慮的方法是考慮如果**你被要求診斷問題,你會得到什麼樣的信息。代碼可以很容易地複製到您自己的IDE中並可以可靠地重現問題,這是最好的起點。 – glytching

+0

謝謝,我一定會牢記這一點 – sharkzooka