0

my issue seems to be very similar to "folder has not yet been created" error when using JUnit temporary folder in testclass.
Unfortunately the provided solution does not work for me and I don't know what to do. I want to test the following:

//@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();
        }
    }
}

When I execute the entire Test Class as well as when I execute the single test, it always throws the exception in the first try catch block..

java.lang.IllegalStateException: the temporary folder has not yet been created
Lavair
  • 888
  • 10
  • 21
  • 1
    Show us the full test class, especially the test header. We don't know if your test class is set up correctly. Also note that Rules are no longer supported in JUnit 5, so both Junit 3 and Junit 5 will have a problem with this. – Joachim Sauer Jan 28 '21 at 13:44
  • @JoachimSauer Is compatibility with Junit3 really a problem any more in 2021? – GhostCat Jan 28 '21 at 14:27
  • @JoachimSauer I've edited the question and added the requested parts. – Lavair Jan 28 '21 at 14:27
  • @GhostCat: I wish it wasn't, but given that I still see plenty of questions asking about Java 7, I can never be sure about it. – Joachim Sauer Jan 28 '21 at 15:03
  • 1
    @JoachimSauer Tell me, we just moved from 8 to 11 for our product to be shipped like end of next year. On the other hand: I still see java 8, 7, 6 questions here ... but not once a junit3 one. I guess updating your unit test environment is easier than updating jvms. – GhostCat Jan 28 '21 at 15:07
  • @Lavair Can you try to remove PowerMockRunner and check whether the rule works then. I remember that there is or was a problem with JUnit rules and PowerMock but I don't remember the details. – Stefan Birkner Feb 02 '21 at 06:39
  • @StefanBirkner i will give it a try in the following days. Found a workaround for now and I give you an update when i tried it out :) – Lavair Feb 02 '21 at 12:52

1 Answers1

1

I use something like this

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Before
public void setup() throws IOException {
    tempFolder.create();
}

@After
public void tearsDown() throws IOException {
    tempFolder.delete();
}

Well, in my class is with static rule, but you get the gist.

All properties and rules and method static, like this

@ClassRule
public static TemporaryFolder tempFolder = new TemporaryFolder();

@BeforeClass
public static void setupAll() throws IOException {
    tempFolder.create();
}

@AfterClass
public static void tearsDownAll() throws IOException {
    tempFolder.delete();
}

Tell me if it works !