java - 为什么没有创建规则临时文件夹?
问题描述
在 testclass 中使用 JUnit 临时文件夹时,我的问题似乎与“尚未创建文件夹”错误非常相似。
不幸的是,提供的解决方案对我不起作用,我不知道该怎么做。我想测试以下内容:
//@RunWith(SpringRunner.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClass.class, SomeClassTests.class})
@SpringBootTest
class SomeClassTests {
@Autowired
private SomeClass systemUnderTest;
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
void somePrivateMethodShouldDeleteSomeFile() {
// given
File foo = null;
try {
foo = folder.newFile("foo.java");
} catch (IOException e) {
assert false;
}
assertThat(foo.exists());
// when
try {
Method privateMethod = SomeClass.class.getDeclaredMethod("somePrivateMethod");
privateMethod.setAccessible(true);
privateMethod.invoke(SomeClass.class);
} catch (Exception e) {
assert false;
}
// then
assertThat(!foo.exists());
}
@TestConfiguration
static class SomeClassTestsConfiguration {
@Bean
public SomeClass systemUnderTest() {
return new SomeClass();
}
}
}
当我执行整个测试类以及执行单个测试时,它总是在第一个 try catch 块中抛出异常。
java.lang.IllegalStateException: the temporary folder has not yet been created
解决方案
推荐阅读
- .net - 如何在其有效期开始之前使用服务器证书
- ruby-on-rails - Rails:NOT NULL Violation:保存明显的循环关联有时缺少外键
- postgresql - PostgreSQL(带递归)
- webgl - Simplex 3D 噪声动画在移动设备上停止
- java - 使用 `IOUtils.toString(containerRequestContext.getEntityStream(),"UTF-8"); 时无法解码特殊字符 `
- c# - 如何在 C# 中将结构列表写入文件?
- python - Sqlalchemy 模拟:UnifiedAlchemyMagicMock:抑制添加/提交
- java - 在 Java 中创建 Adobe XD 背景模糊效果
- ruby - 使用 Adobe PKCS#7 证书在 Origami 中签名的 PDF 验证
- if-statement - 如何仅在页面及其子页面上显示部分内容?十月CMS