【问题标题】:Mockito UnfinishedVerificationExceptionMockito UnfinishedVerificationException
【发布时间】:2019-06-11 07:09:29
【问题描述】:

问题如下。我有几份报告,我想用Mockito 模拟和测试。每份报告都提供相同的UnfinishedVerificationException,到目前为止我没有尝试解决这个问题。以下是所有父母的报告示例之一。

  • 我将any 更改为anyString
  • ReportSaverinterface 更改为 abstract class
  • 添加 validateMockitoUsage 以完成正确的测试
  • StackOverflow 上查看了与Mockito 相关的类似案例

测试:

public class ReportProcessorTest {

    private ReportProcessor reportProcessor;

    private ByteArrayOutputStream mockOutputStream = (new ReportProcessorMock()).mock();

    @SuppressWarnings("serial")
    private final static Map<String, Object> epxectedMaps = new HashMap<String, Object>();

    @Before
    public void setUp() throws IOException {
        reportProcessor = mock(ReportProcessor.class);
        ReflectionTestUtils.setField(reportProcessor, "systemOffset", "Europe/Berlin");
        ReflectionTestUtils.setField(reportProcessor, "redisKeyDelimiter", "#");

        Mockito.doNothing().when(reportProcessor).saveReportToDestination(Mockito.any(), Mockito.anyString());
        Mockito.doCallRealMethod().when(reportProcessor).process(Mockito.any());
    }

    @Test
    public void calculateSales() throws IOException {
        Map<String, Object> processedReport = reportProcessor.process(mockOutputStream);
        verify(reportProcessor, times(1)); // The line that cause troubles
        assertThat(Maps.difference(processedReport, epxectedMaps).areEqual(), Matchers.is(true));
    }

    @After
    public void validate() {
        Mockito.validateMockitoUsage();
    }
}

待测类:

@Component
public class ReportProcessor extends ReportSaver {

    @Value("${system.offset}")
    private String systemOffset;

    @Value("${report.relativePath}")
    private String destinationPathToSave;

    @Value("${redis.delimiter}")
    private String redisKeyDelimiter;

    public Map<String, Object> process(ByteArrayOutputStream outputStream) throws IOException {
        saveReportToDestination(outputStream, destinationPathToSave);
        Map<String, Object> report = new HashMap<>();

        try (InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
             InputStreamReader reader = new InputStreamReader(inputStream)) {

            CSVReaderHeaderAware csvReader = new CSVReaderFormatter(outputStream).headerAware(reader);

            Map<String, String> data;
            while ((data = csvReader.readMap()) != null) {

                String data = data.get("data").toUpperCase();
                Long quantity = NumberUtils.toLong(data.get("quantity"));

                report.put(data, quantity);
            }
        }

        return report;
    }
}

父类:

public abstract class ReportSaver {

    public void saveReportToDestination(ByteArrayOutputStream outputStream, String destinationPathToSave) throws IOException {
        File destinationFile = new File(destinationPathToSave);
        destinationFile.getParentFile().mkdirs();
        destinationFile.delete();
        destinationFile.createNewFile();
        OutputStream fileOutput = new FileOutputStream(destinationFile);
        outputStream.writeTo(fileOutput);
    }
}

模拟:

public class ReportProcessorMock implements GeneralReportProcessorMock {

    private static final String report = ""; // There can be some data in here

    @Override
    public ByteArrayOutputStream mock() {
        byte[] reportBytes = report.getBytes();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(reportBytes.length);
        outputStream.write(reportBytes, 0, reportBytes.length);
        return outputStream;
    }
}

【问题讨论】:

    标签: java unit-testing mocking mockito


    【解决方案1】:

    当您验证时,您验证了 mock 的特定公共方法:

    verify(reportProcessor, times(1)).process(mockOutputStream);
    

    或在适当的情况下使用通配符:

    verify(reportProcessor, times(1)).process(any(ByteArrayOutputStream.class));
    

    【讨论】:

      猜你喜欢
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2011-08-27
      • 2021-12-10
      • 2014-07-27
      • 2015-06-19
      • 1970-01-01
      相关资源
      最近更新 更多