0
我有一個叫SampleClass
的類。這有很多靜態字段,這個類目前在其他一些類中使用。我是Mockito和Power Mockito的新手。我一直在努力嘲笑SampleClass
。你們可以請幫助讓我知道如何嘲諷SampleClass
。使用Mockito和PowerMockito創建模擬對象
我已經在下面提供了SampleClass及其使用細節供您參考。
SampleClass.java:在其它的類SampleClass的
public class SampleClass {
private static CatFactory catFactory = null;
private static String catPath = SampleConfigurationManager.getInstance().getProperty("locale_path", "");
private static URI catURI = (new File(catPath)).toURI();
private static SampleClass catBusinessLogic;
private static Logger logger = Logger.getInstance(SampleClass.class);
private SampleClass() {
initializeCatFactory();
}
public static SampleClass getInstance() {
try{
if(catBusinessLogic == null) {
catBusinessLogic = new SampleClass();
}
}catch(Exception e){
e.printStackTrace();
}
return catBusinessLogic;
}
public Cat getCat(String code) throws CatException {
logger.log(LogLevel.DEBUG, "SampleClass::getCat ENTRY");
Cat countryCat = null;
if (catFactory != null) {
countryCat = catFactory.getCat(code);
}
logger.log(LogLevel.DEBUG, "SampleClass::getCat EXIT");
if(countryCat == null) {
throw new CatException("Failed to create CAT object");
}
return countryCat;
}
public static void initializeCatFactory() throws CatException{
logger.log(LogLevel.DEBUG, "SampleClass::initializeCatFactory ENTRY");
try {
catFactory = new CatFactory(catURI.toURL());
} catch (MalformedURLException mue) {
logger.log(LogLevel.FATAL, "MalformedURLException while creating CATFactory " + mue.toString());
throw new CatException("Failed to create CATFactory");
}
logger.log(LogLevel.DEBUG, "SampleClass::initializeCatFactory EXIT");
}
}
用法:
SampleClass sampleClass = SampleClass.getInstance();
String code = "ABC";
Cat cat = sampleClass.getCat(code);
CATUtil catUtil = new CATUtil(cow);
到底是什麼問題?你在嘲笑上述課程時是否有錯誤? –
是的..請參考我在下面的話題中提到的細節。 –