我们在少数历史项目中使用了PowerMock。不幸的是,PowerMock已经死了,并且与Java 11不兼容。
而且我们正在使用mockStatic()。是的,我们知道它被认为是有害的-它在旧代码中,并且我们不希望现在不重写那些类。
有什么选项可以调整PowerMock以支持Java 11?还是可以轻松地用其他兼容Java 11的框架替换它?(Mockito不支持mockStatic)
我正在尝试使用 powermockito 在我的单元测试用例中模仿文件读取,下面是我想要测试的代码
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class FileOperation {
public List<String> readFromFile( String inputFilePath ) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(inputFilePath));
return lines;
}
public void writeIntoFile(String result, String outputFilePath) throws IOException {
Files.write(Paths.get(outputFilePath), result.getBytes());
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的测试用例文件
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
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;
@RunWith(PowerMockRunner.class) …Run Code Online (Sandbox Code Playgroud)