没有模拟方法如何 return 模拟另一个 class 的实例
without mocking method how to return mock instance of another class
这是我的 Java class:
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);
}
}
}
我想在 class 调用
时模拟和 return 我自己的实例
return new CheckboxKeywordHelper(keywordData);
我用不同的方式尝试这个 ways.But 不适合我。
我无法模拟 createKeywordHelper method.Because 我想检查 Switch-case
我只使用 mockito。并使用 testng,java 8
在这种情况下,我们必须使用 power mockito.Unless 我们不能模拟构造函数。
这是我的 Java class:
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);
}
}
}
我想在 class 调用
时模拟和 return 我自己的实例return new CheckboxKeywordHelper(keywordData);
我用不同的方式尝试这个 ways.But 不适合我。
我无法模拟 createKeywordHelper method.Because 我想检查 Switch-case
我只使用 mockito。并使用 testng,java 8
在这种情况下,我们必须使用 power mockito.Unless 我们不能模拟构造函数。