PowerMock 不会模拟静态方法在 Spring-Boot 应用程序中抛出异常
PowerMock won't mock static method to throw an Exception in a Spring-Boot application
我意识到有很多非常相似的问题。我已经完成了所有这些,但我仍然无法使我的代码正常工作。
我在 Spring-Boot 应用程序中定义了一个服务,就像这样:
@Service
public class FileStorageService {
private final Path fileStorageLocation;
@Autowired
public FileStorageService(final FileStorageProperties fileStorageProperties) {
//FileStorageProperties is a very simple class that right now just holds one String value
this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
.toAbsolutePath()
.normalize();
try {
Files.createDirectories(fileStorageLocation);
} catch (IOException e) {
// FileStorageException is my custom, runtime exception
throw new FileStorageException("Failed to create directory for stored files", e);
}
}
}
我想测试场景,当目录创建失败时,我需要模拟方法 Files.createDirectories()。我的测试 class 看起来像这样:
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;
import java.io.IOException;
import java.nio.file.Files;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Files.class})
public class FileStorageServiceTest {
private static final String UPLOAD_DIR = "uploadDir";
@Test(expected = FileStorageException.class)
public void some_test() throws IOException {
PowerMockito.mockStatic(Files.class);
PowerMockito.when(Files.createDirectories(Mockito.any())).thenThrow(new IOException());
new FileStorageService(createFileStorageProperties());
}
private FileStorageProperties createFileStorageProperties() {
final FileStorageProperties fileStorageProperties = new FileStorageProperties();
fileStorageProperties.setUploadDir(UPLOAD_DIR);
return fileStorageProperties;
}
}
我相信我遵循了我阅读过的教程和问题中的每一步。
我使用:
- @RunWith(PowerMockRunner.class),
- @PrepareForTest({Files.class}),
- PowerMockito.mockStatic(Files.class),
- 和PowerMockito.when(Files.createDirectories(Mockito.any())).thenThrow(new IOException());.
仍然,在测试期间没有抛出异常并且失败。非常感谢您的帮助,因为我觉得我错过了一些非常简单的东西,只是看不到它。
发件人:https://github.com/powermock/powermock/wiki/Mock-System
Normally you would prepare the class that contains the static methods (let's call it X) you like to mock but because it's impossible for PowerMock to prepare a system class for testing so another approach has to be taken. So instead of preparing X you prepare the class that calls the static methods in X!
基本上,我们模拟 class 对系统 class 的使用,而不是不可模拟的系统 class 本身。
@PrepareForTest({Files.class})
在不模拟任何系统的情况下执行此操作的另一种非 Powermock 方法 class 是创建一个辅助方法,@Spy 原始 class,并专门模拟该辅助方法以抛出异常。
when(spy.doSomethingWithSystemClasses()).thenThrow(new Exception("Foo");
我意识到有很多非常相似的问题。我已经完成了所有这些,但我仍然无法使我的代码正常工作。
我在 Spring-Boot 应用程序中定义了一个服务,就像这样:
@Service
public class FileStorageService {
private final Path fileStorageLocation;
@Autowired
public FileStorageService(final FileStorageProperties fileStorageProperties) {
//FileStorageProperties is a very simple class that right now just holds one String value
this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
.toAbsolutePath()
.normalize();
try {
Files.createDirectories(fileStorageLocation);
} catch (IOException e) {
// FileStorageException is my custom, runtime exception
throw new FileStorageException("Failed to create directory for stored files", e);
}
}
}
我想测试场景,当目录创建失败时,我需要模拟方法 Files.createDirectories()。我的测试 class 看起来像这样:
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;
import java.io.IOException;
import java.nio.file.Files;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Files.class})
public class FileStorageServiceTest {
private static final String UPLOAD_DIR = "uploadDir";
@Test(expected = FileStorageException.class)
public void some_test() throws IOException {
PowerMockito.mockStatic(Files.class);
PowerMockito.when(Files.createDirectories(Mockito.any())).thenThrow(new IOException());
new FileStorageService(createFileStorageProperties());
}
private FileStorageProperties createFileStorageProperties() {
final FileStorageProperties fileStorageProperties = new FileStorageProperties();
fileStorageProperties.setUploadDir(UPLOAD_DIR);
return fileStorageProperties;
}
}
我相信我遵循了我阅读过的教程和问题中的每一步。 我使用:
- @RunWith(PowerMockRunner.class),
- @PrepareForTest({Files.class}),
- PowerMockito.mockStatic(Files.class),
- 和PowerMockito.when(Files.createDirectories(Mockito.any())).thenThrow(new IOException());.
仍然,在测试期间没有抛出异常并且失败。非常感谢您的帮助,因为我觉得我错过了一些非常简单的东西,只是看不到它。
发件人:https://github.com/powermock/powermock/wiki/Mock-System
Normally you would prepare the class that contains the static methods (let's call it X) you like to mock but because it's impossible for PowerMock to prepare a system class for testing so another approach has to be taken. So instead of preparing X you prepare the class that calls the static methods in X!
基本上,我们模拟 class 对系统 class 的使用,而不是不可模拟的系统 class 本身。
@PrepareForTest({Files.class})
在不模拟任何系统的情况下执行此操作的另一种非 Powermock 方法 class 是创建一个辅助方法,@Spy 原始 class,并专门模拟该辅助方法以抛出异常。
when(spy.doSomethingWithSystemClasses()).thenThrow(new Exception("Foo");