原文链接:https://www.jianshu.com/p/e39bafff7714
1、 创建一个被测试类:FileUtil
package UT_Test;
import java.io.File;
import java.io.FileNotFoundException;
public class FileUtil {
private static final String LOG_FILE = "root.log";
public boolean mkdirs(String path) {
log("start to mkdirs:" + path);
File fileDir = new File(path);
if (fileDir.exists()) {
throw new IllegalArgumentException(path + "alreay exists!");
} else {
return fileDir.mkdirs();
}
}
public File getLogFile() throws FileNotFoundException {
log("start to get log file");
String logPath = System.getenv("LOG_PATH");
if (null == logPath) {
throw new IllegalArgumentException("System env:LOG_PATH not exist!");
}
if (isExists(logPath)) {
return new File(logPath + LOG_FILE);
}
throw new FileNotFoundException("File " + logPath + LOG_FILE + "not exist!");
}
private boolean isExists(String logPath) {
return false;
}
public void log(String log) {
System.out.println(log);
}
}
2、测试类:FileUtilMkdirTest
package UT_Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* 参考链接:
* https://www.jianshu.com/p/e39bafff7714
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(FileUtil.class)
public class FileUtilMkdirTest {
@Before
public void init() throws Exception {
File fileDir = PowerMockito.mock(File.class);
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(fileDir);
PowerMockito.when(fileDir.exists()).thenReturn(false);
PowerMockito.when(fileDir.mkdirs()).thenReturn(true);
}
@Test
public void testMkdirs() {
assertTrue(new FileUtil().mkdirs("testDir"));
}
@Test
public void testGetLogFileWithException() {
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getenv("LOG_PATH")).thenReturn("./");
try {
new FileUtil().getLogFile();
fail();
} catch (FileNotFoundException e) {
assertTrue(true);
}
}
@Test
public void testGetLogFile() throws Exception {
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getenv("LOG_PATH")).thenReturn("./");
FileUtil fileUtil = PowerMockito.spy(new FileUtil());
PowerMockito.when(fileUtil, "isExists", "./").thenReturn(true);
File logFile = fileUtil.getLogFile();
assertTrue(logFile != null);
}
@Test
public void testGetLogFileWithoutLog() throws Exception {
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.getenv("LOG_PATH")).thenReturn("./");
FileUtil fileUtil = PowerMockito.spy(new FileUtil());
PowerMockito.when(fileUtil, "isExists", "./").thenReturn(true);
PowerMockito.doNothing().when(fileUtil, "log", Mockito.anyString());
File logFile = fileUtil.getLogFile();
assertTrue(logFile != null);
}
}
3、 相关链接:
(1) JUnit单元测试5—PowerMock https://www.jianshu.com/p/e39bafff7714 (2) 单元测试之PowerMock https://www.cnblogs.com/kancy/p/13912443.html
|