Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
knezmilos13 opened this issue Sep 20, 2017 · 4 comments
Open
Labels

Comments

@knezmilos13
Copy link

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?

@woprzech
Copy link
Contributor

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.

@knezmilos13
Copy link
Author

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

@zbychupragmatists
Copy link
Contributor

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.

@jfs
Copy link

jfs commented Oct 16, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants