JUnitParams icon indicating copy to clipboard operation
JUnitParams copied to clipboard

Allow running @Before and/or @BeforeClass before generating parameters

Open knezmilos13 opened this issue 8 years ago • 4 comments

I often use objects that I instantiate in @Before or @BeforeClass in my tests; the problem is that the method that generates parameters for test is called before @BeforeClass and @Before. For example, all my "task" objects will be null here:

    @Before
    public static void setUp() {
        task1_1 = new ApiTaskFakeImpl(OP_NAME_1);
        task1_2 = new ApiTaskFakeImpl(OP_NAME_1);
        task2 = new ApiTaskFakeImpl(OP_NAME_2);
        task3 = new ApiTaskFakeImpl(OP_NAME_3);
        task4 = new ApiTaskFakeImpl(OP_NAME_4);
    }

...

private Object[] parametersForGetRequestsForIdentifiers() {
        return new Object[]{
                new Object[]{task1_1},
                new Object[]{task1_2},
                new Object[]{task2},
                new Object[]{task3},
                new Object[]{null}
        };

Could there be a way to request that the method be called after @Before/@BeforeClass? Perhaps using an additional parameter on @Parameters annotation?

knezmilos13 avatar Sep 20 '17 10:09 knezmilos13

No, method which creates parameters runs before any @Before methods. JUnitParams just generates new test case for every parameter set but doesn't interfere in JUnit test's lifecycle, so @Before method is running for every param set.

woprzech avatar Sep 20 '17 10:09 woprzech

Right, so this issue I opened is basically saying "Could we have that in a future version, please?"

knezmilos13 avatar Sep 20 '17 11:09 knezmilos13

Let's make it clear @Before annotation is for setup of objects used in test, while parameters method for setting up parameters for them. If your tests works the way you need to consider order of execution @Before method and parameters method that means your doing something wrong. I personally never had that problem and I used junitparams a lot in couple of projects. Please provide your real code and we will try to help you refactor it.

zbychupragmatists avatar Sep 20 '17 13:09 zbychupragmatists

@knezmilos13, I agree with @woprzech and @zbychupragmatists that @Parameters must be called before @Before. Their explanation isn't clear at first glance though. JUnitParams must call @Parameters first so that it knows how many tests is needs to execute and what the parameters for each execution are. It will then call @Before before each individual test execution for each set of parameters. I agree that it's unintuitive at first glance, but it makes sense when you actually think about what is happening.

However, this logic doesn't hold when you take @BeforeClass into account. I cannot see an argument made that a developer should expect @Parameters which are tied to test executions to run before a method annotated with @BeforeClass which is run before all tests are executed. I think that @BeforeClass annotated methods should be executed before @Parameters annotations are evaluated.

jfs avatar Oct 16 '17 15:10 jfs