我是新来mockito测试,我一直在试图跟随博客,但它现在非常混乱。Mockito的环境getExternalStorageState()android
座右铭是测试FileUtility
类中的两个静态函数。代码如下。
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public static String getLogFilePath(String fileName) {
if (isExternalStorageWritable()) {
File _file;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
_file = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS);
if (exists(_file)) {
return getAbsoluteFilePath(_file,fileName);
}
}
_file = Environment.getExternalStorageDirectory();
if (exists(_file)) {
return getAbsoluteFilePath(_file,fileName);
}
}
File _file = Environment.getDataDirectory();
if (exists(_file)) {
return getAbsoluteFilePath(_file,fileName);
}
return fileName;
}
private static boolean exists(File file) {
return file != null && file.exists();
}
我该如何编写单元测试用例来测试输入如“fileName = null”的函数?
请分享一些光/代码到它。
你不能用mockito测试静态函数。尝试使用PowerMock –