我正在尝试测试抛出的异常,但代码继续返回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;
答案 0 :(得分: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,考虑到这些假设:
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就足够了。