WrongTypeOfReturnValue mockito

时间:2016-12-04 07:15:48

标签: java unit-testing mockito

我在我正在模拟的类中有一个方法,它返回一个布尔值。

@Service
    public boolean writeToHDFS(HashMap<String, String> tableHistoryMap, String text, String tableName, String siteName, String customer,
          boolean incremental){
        try{
          String absoluteFilepath = hdfsService.generateHDFSAbsolutePathNoDate(tableHistoryMap, tableName, siteName, customer, incremental);
          Path writeFile = new Path(absoluteFilepath);

      if (!hdfsService.HDFSFileExists(writeFile)) {
        writer = hdfs.create(writeFile, false);
        logger.info("hdfs file create set up");
      }
      else {
        writer = hdfs.append(writeFile);
        logger.info("hdfs file append set up");
        //TODO: WRITING HERE
        //IF stripping capability is enabled, strip headers then
        text = hdfsService.parseText(stripHeaders, text, tableName);
      }
      if(hdfsService.writerHDFS(writer,text)){
        return true;
      }

    }
    catch(Exception ex){
      logger.error(ex.getMessage());
    }
    return false;
  }



@MockBean
WriteDataIntoHDFSService writeDataIntoHDFSService
@Test
  public void testDepositData(){
    KafkaMessage kafkaMessage = new KafkaMessage();
    kafkaMessage.setInitialLoadRunning(true);
    kafkaMessage.setCustomer("customer");
    kafkaMessage.setMessageContent("message");
    kafkaMessage.setTableName("table");
    kafkaMessage.setInitialLoadComplete(false);
    HashMap<String, String> tableMap = new HashMap<>();
    tableMap.put("Test","Test");

    when(writeDataIntoHDFSService.writeToHDFS(tableMap, kafkaMessage.getMessageContent(), kafkaMessage.getTableName(), "Site", kafkaMessage.getCustomer(),
       false)).thenReturn(true);

    Assert.assertTrue(kafkaService.depositData(kafkaMessage,"test",kafkaMessage.getCustomer(),tableMap));
  }

断言失败并且我调试了它,它在when()... thenReturn(true)时返回WrongTypOfReturnValue。它说toString()不能返回布尔值。

日志:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Boolean cannot be returned by toString()
toString() should return String
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

dataDeposit:

public boolean depositData(KafkaMessage kafkaMessage, String topic, HashMap<String, String> tableHistoryMap){
   String customer = getCustomer(topic);
    String site = getSite(topic);
    if (kafkaMessage.isInitialLoadRunning() && !kafkaMessage.isInitialLoadComplete()) {
      //runInitial(kafkaMessage, splitKafkaTopic[2]);
      if (writeDataIntoHDFSService
          .writeToHDFS(tableHistoryMap, kafkaMessage.getMessageContent(), kafkaMessage.getTableName(), site,
              kafkaMessage.getCustomer(), false)) {
        logger.info("Wrote initial data into HDFS file for: " + kafkaMessage.getTableName());

        synchronized (lock) {
          kafkaConsumer.commitSync();
          logger.info("Kafka commit sync done for initial load.");
        }
         return true;
      }
    }
return false;

}

更新:甚至不确定我做了什么,但它现在似乎正在运作......

0 个答案:

没有答案