gpt4 book ai didi

java - PowerMockito如何在构造函数调用时抛出异常

转载 作者:行者123 更新时间:2023-12-03 00:44:04 25 4
gpt4 key购买 nike

我正在使用反射测试以下私有(private)方法(getPrintWriter):

public abstract class CsvAuditor extends Auditor {

public void run() {
PrintWriter writer = getPrintWriter();
if (writer == null)
return;
writeReport(writer);
writer.close();
}

private PrintWriter getPrintWriter() {
PrintWriter writer;
try {
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
Date date = new Date();
writer = new PrintWriter("report__" + dateFormat.format(date) + ".csv");
}
catch (FileNotFoundException ex) {
System.err.println(ex.getMessage());
return null;
}
return writer;
}

}

我想模拟 PrintWriter 的构造函数抛出 FileNotFoundException。

@RunWith(PowerMockRunner.class)
@PrepareForTest({CsvAuditor.class, PrintWriter.class})
public class CsvAuditorTest {

private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

@Before
public void setUpStreams() {
System.setErr(new PrintStream(errContent));
}

@After
public void cleanUpStreams() {
System.setErr(null);
}

@Test
public void shouldPrintErrorIfFileIsInaccessible() throws Exception {
//given
Method getPrintWriter = getPrivateMethod("getPrintWriter", new Class[]{});
CsvAuditor csvAuditor = new CsvAuditor() {
@Override
protected void writeReport(PrintWriter writer) {

}
};

//Here is line where RuntimeException is thrown
PowerMockito.whenNew(PrintWriter.class).withAnyArguments().thenThrow(new FileNotFoundException());


//when
Object result = getPrintWriter.invoke(csvAuditor);

//then
assertNull(result);
assertEquals("File is inaccessible\r\n", errContent.toString());
}

private Method getPrivateMethod(String methodName, Class[] argTypes) throws NoSuchMethodException {
Method method = CsvAuditor.class.getDeclaredMethod(methodName, argTypes);
method.setAccessible(true);
return method;
}
}

我想模拟 PrintWriter 构造函数被抛出 FileNotFoundException,然后我想断言,返回了 null。我标记了一条注释行,它抛出以下异常,但我不知道如何正确模拟它。

> java.lang.RuntimeException: PowerMock internal error  at
> org.powermock.api.mockito.internal.expectation.DelegatingToConstructorsOngoingStubbing$InvokeStubMethod.invoke(DelegatingToConstructorsOngoingStubbing.java:133)
> at
> org.powermock.api.mockito.internal.expectation.DelegatingToConstructorsOngoingStubbing.thenThrow(DelegatingToConstructorsOngoingStubbing.java:66)
> at
> auditor.csv.CsvAuditorTest.shouldPrintErrorIfFileIsInaccessible(CsvAuditorTest.java:46)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
> at
> org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
> at
> org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl$PowerMockJUnit49MethodRunner.executeTestInSuper(PowerMockJUnit49RunnerDelegateImpl.java:116)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl$PowerMockJUnit49MethodRunner.executeTest(PowerMockJUnit49RunnerDelegateImpl.java:77)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
> at
> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
> at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
> at
> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
> at
> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
> at
> org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
> at
> org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
> at
> org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
> at
> org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
> at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
> at
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
> at
> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
> Caused by: java.io.FileNotFoundException at
> org.powermock.api.mockito.internal.expectation.DelegatingToConstructorsOngoingStubbing$InvokeStubMethod.invoke(DelegatingToConstructorsOngoingStubbing.java:130)
> ... 34 more

最佳答案

使用精确参数匹配器尝试下面

PowerMockito.whenNew(PrintWriter.class).withArguments(Matchers.anyString()).thenThrow(new FileNotFoundException());

我得到的结果对象为空。

关于java - PowerMockito如何在构造函数调用时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31993783/

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