I have some JUnit tests that use the TemporaryFolder
@Rule
. They use the TemporaryFolder
in a @Before
method to perform some setup:
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Before
public void init() {
folder.newFile("my-file.txt");
}
@Test
public void myTest() { ... }
Most of the time this works perfectly. However, when using the SpringJUnit4ClassRunner
I find that in some cases the init()
method is invoked before the Statement
inside my TemporaryFolder
instance is applied. Because of this, the temporary folder location is unset (i.e: null
) when folder
is used within init()
and my file ends up in the working directory, not /tmp
.
So in some cases @Before
methods are executed ahead of the rules, however, I can't establish a definite pattern. I occasionally see a similar problem with some of my own rule implementations.
Is there any way that I can ensure that my rule statements are applied before any setup methods?