方法不是gettting mocked(Mockito.when()。thenReturn())并且没有返回指定的模拟值

时间:2017-03-24 05:21:22

标签: java unit-testing exception junit mockito

我正在为受保护的方法编写测试用例,该方法称为

protected boolean printMultipleImages(int rows, int cols, IIOImage[] imgs,
                                      PrintMode mode, AutomaticCroppingOption crop,
                                      double reduction, int renderOption)

在此方法中,数组变量正在初始化,下面的代码显示了

RenderRequest request = this.formSlotsRenderRequest(rows, cols, imgs,mode, crop, reduction);
        // Create a Printer Properties Object...
        PrinterProperties prop = this.getAutoPrinterProperties();
        // Get the Appropriate Print Rendering Engine..
        MultiFilmPrintRenderer engine = this.getPrintRenderer();

        MultiFilmRenderRequest multiFilmRenderRequest = new MultiFilmRenderRequest(renderOption, request,new ChoppingPreferences());

        assert engine != null;

        RenderedImage[] rImageArr =engine.renderAcrossFilms(multiFilmRenderRequest, prop); //NPE here, rImageArr=null, though I mocked the method to return something.

在下面突出显示的行中,变量rImageArr未初始化并保持为null, 方法调用engine.renderAcrossFilms(multiFilmRenderRequest, prop);给我null,即使我模拟它返回一些东西,控件进入方法并给出空指针异常,这不应该发生,因为我在嘲笑它。这里使用的所有变量都是方法的本地变量。

RenderedImage[] rImageArr =engine.renderAcrossFilms(multiFilmRenderRequest, prop);

这就是我嘲笑方法engine.renderAcrossFilms(multiFilmRenderRequest, prop);

的方法
@Test
    public void testPrintMultipleImages() throws Exception
    {
        //MultiFilmRenderRequest req=Mockito.mock(MultiFilmRenderRequest.class);
        MultiFilmPrintRenderer engine=Whitebox.getInternalState(autoPrintEndPoint, "_engine");//new JCPrintRenderEngine();//
        System.out.println(engine+"here");

        RenderedImage[] renderAcrossFilms=new RenderedImage[]{Mockito.mock(RenderedImage.class)};
        PrinterProperties prop=Mockito.mock(PrinterProperties.class);
        RenderRequest renderRequest=Mockito.mock(RenderRequest.class);
        AutomaticCroppingOption crop=Mockito.mock(AutomaticCroppingOption.class);
        PrintMode mode=Mockito.mock(PrintMode.class);
        IIOImage img=Mockito.mock(IIOImage.class);

        MultiFilmRenderRequest multiFilmRenderRequest = new MultiFilmRenderRequest(1, renderRequest,
                                     new ChoppingPreferences());

        PowerMockito.doNothing().when(autoPrintEndPoint,PowerMockito.method(AutoPrintEndPoint.class,"loadImage", IIOImage.class)).withArguments(img);
        PowerMockito.doReturn(renderRequest).when(autoPrintEndPoint,PowerMockito.method(AutoPrintEndPoint.class,"formSlotsRenderRequest", int.class, int.class, IIOImage[].class,PrintMode.class,AutomaticCroppingOption.class,double.class)).withArguments(2,2,new IIOImage[]{img},mode,crop,12.5);
        PowerMockito.doReturn(prop).when(autoPrintEndPoint,PowerMockito.method(AutoPrintEndPoint.class,"getAutoPrinterProperties")).withNoArguments();
              Mockito.when(engine.renderAcrossFilms(Mockito.any(MultiFilmRenderRequest.class),Mockito.any(PrinterProperties.class))).thenReturn(renderAcrossFilms);

        Method printMultipleImages=AutoPrintEndPoint.class.getDeclaredMethod("printMultipleImages", int.class,int.class,IIOImage[].class,PrintMode.class,AutomaticCroppingOption.class,double.class,int.class);
        printMultipleImages.setAccessible(true);
        printMultipleImages.invoke(autoPrintEndPoint,2,2,new IIOImage[]{img},mode,crop,12.5,1);

    }

我希望我的问题清楚,我希望你能帮助我

3 个答案:

答案 0 :(得分:0)

在您要测试的方法中, 它创建了新实例

MultiFilmRenderRequest multiFilmRenderRequest = new MultiFilmRenderRequest(renderOption, request,new ChoppingPreferences());

,这将用作模拟方法的参数。 但是您可能已经模拟了提供参数的方法,该参数不等于printMultipleImages方法传递给引擎的参数。

除此之外,我可以看到 PrinterProperties prop = Mockito.mock(PrinterProperties.class);

但是这个mock类还没有被使用,所以目标测试方法也可能会发送一个不同的PrinterProperties对象。

你可以使用

engine.renderAcrossFilms(Mockito.any(MultiFilmRenderRequest.class),Mockito.any(PrinterProperties.class))

答案 1 :(得分:0)

我将继续扩展此答案,因为您提供了更多详细信息,但根据您的最新更新,您有:

MultiFilmPrintRenderer engine =
    Whitebox.getInternalState(autoPrintEndPoint, "_engine");
// ...
Mockito.when(engine.renderAcrossFilms(multiFilmRenderRequest, prop))
    .thenReturn(renderAcrossFilms);

这个Whitebox.getInternalState是否返回了一个模拟对象?

答案 2 :(得分:0)

我在engine对象上创建了一个模拟,用于调用我试图模拟的方法并执行setInternalState。

MultiFilmPrintRenderer engine = Mockito
                .mock(MultiFilmPrintRenderer.class);
Whitebox.setInternalState(autoPrintEndPoint, "_engine", engine);

这样就设置了模拟对象。 NPE得到解决