没有mocking方法如何返回另一个类

时间:2018-02-15 05:58:33

标签: methods java-8 mocking instance

这是我的Java类:

public final class KeywordHelpers{
  private KeywordHelpers(){}

  public static KeywordHelper createKeywordHelper(KeywordData keywordData){
   try{
    switch (keywordData.tooltype){
    case ToolType.CheckboxConfigurableTool:
      return new CheckboxKeywordHelper(keywordData);
    case ToolType.BloodPressureTool:
      return new BloodPressureKeywordHelper(keywordData);
    case ToolType.FixValueTool:
      return new FixValueKeywordHelper(keywordData);
    case ToolType.NumericTool:
    return new NumericKeywordHelper(keywordData);
    default:
      throw new IllegalArgumentException("Unsupported keyword tool type: '" + Short.toString(keywordData.tooltype) +
      "' in keyword with id '" + keywordData.versioned.id + "' and term '" + keywordData.term + "'.");
    }
  }catch (SpiderException e){
    throw new IllegalStateException("Could not read keyword information for keyword with id '" + keywordData.versioned.id + "' and term '" + keywordData.term + "'.", e);
  }catch (XMLStreamException e){
    throw new IllegalStateException("Could not read keyword information for keyword with id '" + keywordData.versioned.id + "' and term '" + keywordData.term + "'.", e);
  }
 }
}

我想在这个类调用

时模拟并返回我自己的实例
  

返回新的CheckboxKeywordHelper(keywordData);

我以不同的方式尝试。但不为我工作。

我无法模拟createKeywordHelper方法。因为我想查看Switch-case

我只使用mockito。并使用testng,java 8

1 个答案:

答案 0 :(得分:1)

在这种情况下,我们必须使用power mockito。除非我们不能模拟构造函数。