通缉但不调用:Mockito PrintWriter

您好,我正在开发一个项目并使用 PrintWriter 类来打开和写入文件.但是当我编写相同的测试用例时,它会出现以下错误在第 153 行

Hi I am working on a project and using PrintWriter class for opening and writing in the file. But when I am writing the test case for same it gives following error at Line 153

Wanted but not invoked:
  mockPrintWriter.println("ID    url1
  ");
 -> at x.y.z.verify(ProcessImageDataTest.java:153)
 Actually, there were zero interactions with this mock.

代码:(使用 Lombok 库)

Code: (Uses Lombok Library)

ProcessImageData.java

ProcessImageData.java

@Setter
@RequiredArgsConstructor
public class ProcessImageData implements T {
    private final File newImageDataTextFile;

   @Override
    public void execute() {
       LineIterator inputFileIterator = null;
       try {
        File filteredImageDataTextFile = new File(filteredImageDataTextFilepath);

          PrintWriter writer = new PrintWriter(newImageDataTextFile);
          inputFileIterator = FileUtils.lineIterator(filteredImageDataTextFile, StandardCharsets.UTF_8.displayName());
          while (inputFileIterator.hasNext()) {
                    if(someCondition)
           **Line51**           writer.println(imageDataFileLine);
                    //FileUtils.writeStringToFile(newImageDataTextFile, imageDataFileLine + NEWLINE, true);
                }
            }          

      } catch (Exception e) {

      } finally {
          LineIterator.closeQuietly(inputFileIterator);
 **LINE63**         writer.close();
      }
  }

ProcessImageDataTest.java

ProcessImageDataTest.java

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ProcessImageData.class, FileUtils.class, Printwriter.class })
public class ProcessImageDataTest {

private ProcessImageData processImageData;

private static final String FILTERED_IMAGE_DATA_TEXT_FILE_PATH = "filteredFilepath";
private File FILTEREDFILE = new File(FILTERED_PATH);
private static final File IMAGE__FILE = new File("imageFilePath");

private LineIterator lineIterator;
@Mock
private PrintWriter mockPrintWriter;

@Before
public void init() throws Exception {
    MockitoAnnotations.initMocks(this);

    processImageData = new ProcessImageData(Palettes_file, FILTERED_PATH, IMAGE_FILE);

    PowerMockito.mockStatic(FileUtils.class);
    PowerMockito.whenNew(PrintWriter.class).withArguments(IMAGE_FILE).thenReturn(mockPrintWriter);

    PowerMockito.when(FileUtils.lineIterator(FILTERED_FILE, StandardCharsets.UTF_8.displayName())).thenReturn(lineIterator);
    PowerMockito.when(lineIterator.hasNext()).thenReturn(true, true, false);

}

@Test
public void testTaskWhenIDInDBAndStale() throws IOException {
    PowerMockito.when(lineIterator.nextLine()).thenReturn(ID2 + SPACE + URL1, ID1 + SPACE + URL2);

    processImageData.execute();

    List<String> exepctedFileContentOutput = Arrays.asList(ID2 + SPACE + URL1 + NEWLINE);
    verify(exepctedFileContentOutput, 1, 1);
}


@Test
public void testTaskWhenIDNotInDB() throws IOException {
    PowerMockito.when(lineIterator.nextLine()).thenReturn(ID2 + SPACE + URL1, ID3 + SPACE + URL2);

    processImageData.execute();

    List<String> exepctedFileContentOutput = Arrays.asList(ID3 + SPACE + URL2 + NEWLINE);
    verify(exepctedFileContentOutput, 1, 1);
}

private void verify(List<String> exepctedFileContentOutput, int fileWriteTimes, int fileReadTimes) throws IOException {
    for (String line : exepctedFileContentOutput){
**Line153**                     Mockito.verify(mockPrintWriter,  Mockito.times(fileWriteTimes)).print(line);  
    }

    PowerMockito.verifyStatic(Mockito.times(fileReadTimes));
    FileUtils.lineIterator(FILTERED_IMAGE_DATA_TEXT_FILE, StandardCharsets.UTF_8.displayName());
}

}

我也在模拟 PrintWriter 的新运算符,使用 bean 进行注入.我在做什么错?我被卡住了很长时间并且没有收到错误?任何帮助表示赞赏.

I am mocking a new operator for PrintWriter also, injecting using beans. What is the mistake I am doing?? I am stuck on it from long time and not getting the error? Any help is appreciated.

更新:

我做了下面建议的更改并更新了代码,但现在我得到了错误:

I did changes suggested below and updated the code, but now I get the error:

Wanted but not invoked: mockPrintWriter.print("ASIN2 url1 "); -> 
at softlines.ctl.ruleExecutor.tasks.ProcessImageDataTest.verify‌​(ProcessImageDataTes‌​t.java:153) 
However, there were other interactions with this mock: -> at softlines.ctl.ruleExecutor.tasks.ProcessImageData.execute(Pr‌​ocessImageData.java:‌​51) ->  
at  softlines.ctl.ruleExecutor.tasks.ProcessImageData.execute(Pr‌​ocessImageData.java:‌​51) -> 
at softlines.ctl.ruleExecutor.tasks.ProcessImageData.execute(Pr‌​ocessImageData.java:‌​58) –

推荐答案

我在你的测试中发现了 3 个问题:

I see 3 issues in your test:

  1. 您不要尝试模拟 正确的构造函数,实际上在 execute 方法中,您创建的 PrintWriter 仅使用 File 类型的一个参数,而您尝试模拟 具有 2 个参数的构造函数 一个 File 类型和另一个类型字符串.
  1. You don't try to mock the correct constructor, indeed in the method execute, you create your PrintWriter with only one argument of type File while you try to mock the constructor with 2 arguments one of type File and the other one of type String.

所以代码应该是:

PowerMockito.whenNew(PrintWriter.class)
    .withArguments(IMAGE_FILE)
    .thenReturn(mockPrintWriter);

  1. 为了能够模拟构造函数,您需要准备创建实例的类,在这种情况下为 ProcessImageData,因此您需要添加 ProcessImageData.class 在注解 @PrepareForTest 中.(我不确定那里是否需要 ProcessImageDataTest.class)

  1. To be able to mock a constructor you need to prepare the class creating the instance which is ProcessImageData in this case, so you need to add ProcessImageData.class in the annotation @PrepareForTest. (I'm not sure ProcessImageDataTest.class is needed there)

lineIterator 字段应使用 @Mock 进行注释.

The field lineIterator should be annotated with @Mock.

您应该直接验证 println 无需换行,而不是用新行验证 print,这样更不容易出错.

Instead of verifying print with a new line, you should verify directly println without new line it is much less error prone.

<小时>

我简化了你的代码来展示这个想法.


I simplified your code to show the idea.

假设 ProcessImageData 是:

public class ProcessImageData {

    private final File newImageDataTextFile;

    public ProcessImageData(final File newImageDataTextFile) {
        this.newImageDataTextFile = newImageDataTextFile;
    }

    public void execute() throws Exception{
        try (PrintWriter writer = new PrintWriter(newImageDataTextFile)) {
            LineIterator inputFileIterator = FileUtils.lineIterator(
                newImageDataTextFile, StandardCharsets.UTF_8.displayName()
             );
            while (inputFileIterator.hasNext()) {
                writer.println(inputFileIterator.nextLine());
            }
        }
    }
}

我的单元测试将是:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ProcessImageData.class, FileUtils.class})
public class ProcessImageDataTest {

    private File file = new File("imageFilePath");

    private ProcessImageData processImageData;
    @Mock
    private PrintWriter mockPrintWriter;
    @Mock
    private LineIterator lineIterator;

    @Before
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);

        processImageData = new ProcessImageData(file);
        PowerMockito.whenNew(PrintWriter.class)
            .withArguments(file)
            .thenReturn(mockPrintWriter);
        PowerMockito.mockStatic(FileUtils.class);
        PowerMockito.when(
            FileUtils.lineIterator(file, StandardCharsets.UTF_8.displayName())
        ).thenReturn(lineIterator);
        PowerMockito.when(lineIterator.hasNext()).thenReturn(true, true, false);
    }

    @Test
    public void testExecute() throws Exception {
        PowerMockito.when(lineIterator.nextLine()).thenReturn("Foo", "Bar");
        processImageData.execute();
        Mockito.verify(mockPrintWriter,  Mockito.times(1)).println("Foo");
        Mockito.verify(mockPrintWriter,  Mockito.times(1)).println("Bar");
    }
}

更多详情请参考如何模拟新对象的构造.

For more details please refer to How to mock construction of new objects.

如何在 writer.close 的单元测试中添加验证?

how can I add verification in unit test for writer.close?

一种方法是通过将下一行添加到单元测试中来简单地检查 close() 是否被调用:

One way could be to simply check that close() at be called once by adding the next line to your unit test:

Mockito.verify(mockPrintWriter,  Mockito.times(1)).close();

相关文章