question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@Parameters method is executed before @ClassRule initialization. Could it be the way around?

See original GitHub issue

I have the following problem (using junit 4.11):

    @ClassRule
    public static TemporaryFolder tmp = new TemporaryFolder();
    ...
    @Parameters
    public static Collection<Object[]> data() throws Exception {
        return java.util.Arrays.asList(new Object[][] {
            {0, tmp.getRoot().getPath()}
        });
    }

This results in initializationError

java.lang.IllegalStateException: the temporary folder has not yet been created
    at org.junit.rules.TemporaryFolder.getRoot(TemporaryFolder.java:127)

So seems the @Parameters method is executed before the ClassRule initialization phase which makes scenarios like above one a bit complicated.

Issue Analytics

  • State:open
  • Created 10 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
javornikolovcommented, Oct 11, 2013

Current workaround:

    protected static TemporaryFolder initStaticTemp() {
        try {
            return new TemporaryFolder() { { before(); } };
        } catch (Throwable t) {
            throw new RuntimeException(t);
        }
    }

    public static TemporaryFolder tmp = initStaticTemp();

    @AfterClass
    public static cleanup() throws Exception {
        tmp.delete();
    }

It works but it needs that manual cleanup…

0reactions
balrajdachacommented, May 11, 2021

I tried something like this…

@RunWith(StepRunner.class) public class LaunchEnv { @ClassRule public static final CustomRunner tempFolder = new CustomRunner();

@Test
public void dummyTest() {
}

}

Call JUnitCore.runClasses(LaunchEnv.class); In the @Parameterized.Parameters method

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - @Parameters method is executed before @ClassRule ...
Parameters method is executed before @ClassRule initialization. Could it be the way around?
Read more >
parameters method is executed before @beforeclass method
I would use just plain old java static {..} initializer instead of @BeforeClass, e.g.: @RunWith(Parameterized.class) public class ...
Read more >
Guide to JUnit 4 Rules - Baeldung
We'll begin by introducing the JUnit Rules Model before walking through the most important base rules provided by the distribution.
Read more >
Don't be lazy, use @Rules - Nicola Corti
In this way you can compose multiple helper classes and combine all the ... An Annotation to run a method before all the...
Read more >
JGiven User Guide
JGiven can be used together with JUnit or TestNG, here we assume you ... Note that such methods are executed before the injection...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found