4

リフレクションを使用して次のプライベート メソッド (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;
    }

}

FileNotFoundException をスローする PrintWriter のコンストラクターをシミュレートしたいと考えています。

@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
4

2 に答える 2

0

これを達成するための別のアプローチを次に示します。

whenNew(PrintWriter.class).withParameterTypes(String.class).
    withArguments(Matchers.anyString()).
    thenAnswer(new Answer<Object>() {
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        throw new Exception();
      }
    });
于 2016-07-19T16:41:05.440 に答える