IUnknown
IUnknown

Reputation: 9839

Specific setup for each junit test

I have a sequence of tests which have to be fed an input data in the form of a file.However,the exact data content to be fed into each would be specific.
I intend to use temporary files to achieve this.
The Setup method does not take a parameter.
SO ,what could be done so that the setup can be made to read a specific fragment for each specific test.
The actual set of steps in Setup would be same - creating a temporary file,but with a specific tailored piece of data.

Upvotes: 1

Views: 1034

Answers (1)

Mureinik
Mureinik

Reputation: 312219

Setup methods (i.e., methods annotated with @Before) are designed for running the same steps before every test case. If this isn't the behavior you need, just don't use them.

At the end of the day, a JUnit test is just Java - you could just have a method that takes a parameter and sets up the test accordingly and call it explicitly with the different arguments you need:

public class MyTest {

    private void init(String fileName) {
        // Reads data from the file and sets up the test
    }

    @Test
    public testSomething() {
        init("/path/to/some/file");
        // Perform the test and assert the result
    }

    @Test
    public testSomethingElse() {
        init("/path/to/another/file");
        // Perform the test and assert the result
    }
}

Upvotes: 3

Related Questions