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);
到底是什么问题?你在嘲笑上述课程时是否有错误? –
是的..请参考我在下面的话题中提到的细节。 –