gpt4 book ai didi

java - PowerMockito doThrow 不抛出异常

转载 作者:太空宇宙 更新时间:2023-11-04 11:11:19 26 4
gpt4 key购买 nike

我正在尝试测试抛出的异常,但代码继续返回 true,而不是捕获异常并返回 false。

测试类

@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;
}

每次尝试运行此程序时,我都会收到断言错误,我已经尝试了 do throw 和 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;

最佳答案

我对这个问题还是有点困惑。具体来说:

  • @Before 方法中的测试实例 arg = new Argument(),这意味着 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,考虑到这些假设:

  • update() 方法是:

    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 就足够了。

关于java - PowerMockito doThrow 不抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45979022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com