有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么不创建规则临时文件夹?

我的问题似乎与"folder has not yet been created" error when using JUnit temporary folder in testclass非常相似
不幸的是,提供的解决方案不适用于我,我不知道该怎么办。 我想测试以下内容:

//@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

共 (0) 个答案