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

Parameters on BeforeClass #85

Closed
devkhan opened this issue Jul 1, 2016 · 8 comments
Closed

Parameters on BeforeClass #85

devkhan opened this issue Jul 1, 2016 · 8 comments

Comments

@devkhan
Copy link

devkhan commented Jul 1, 2016

Current Scenario: I initialize some variables in BeforeClass method and keep them as members. Since, I do some processing on files, to optimize tests, I just do them once in the @BeforeClass method, and have asserts in different tests.

Requirement: I want to test multiple files, and hence was thinking may be there is some way I can call BeforeClass with each file and call all of the tests again with the new members.

Is there any way to do this? Maybe some class level params?

@devkhan
Copy link
Author

devkhan commented Jul 1, 2016

Update: I digged a bit more and faced a java.lang.IllegalArgumentException: wrong number of arguments in class configuration.

This happened while trying to pass params to @BeforeClass methods.

@jrmichael
Copy link
Contributor

JUnitParams supports parameterization only of test methods.

@BeforeClass is meant to be a one time action that is supposed to prepare system for tests. I think parameterizing it is not a good idea.

In your case I would suggest to creata a method that provides all the parameters (file names) and use it in your BeforeClass preparation and as a parameters provider for tests. @Parameters(method="myMethodReturningListOfFiles")

@devkhan
Copy link
Author

devkhan commented Jul 1, 2016

I get this, but the issue is if I do this, then all of my tests will repeat the file processing which can be just done at a class(or instance) level. That is why I was initializing members in @BeforeClass.

Anyway, I guess that is by JUnit's design, I'd have to try something else. Thanks.

@jrmichael
Copy link
Contributor

jrmichael commented Jul 1, 2016

Maybe this snippet will help you

@RunWith(JUnitParamsRunner.class)
public class MyTestClass {

    private static File[] files;

    @BeforeClass
    public static void prepareFiles() {
        files = loadMyFiles("file1", "file2");
    }

    public File[] getFiles() {
        return files;
    }

    @Test
    @Parameters(method = "getFiles")
    public void doSomeTesting(File file) {
        //testing...
    }
}

EDIT: this snippet won't work - @BeforeClass is called during test execution and parameters are evaluated earlier

@devkhan
Copy link
Author

devkhan commented Jul 1, 2016

Thanks, I'm doing something similar to this. I assume you mean @BeforeClass there (instead of @BeforeEach, right?

@devkhan
Copy link
Author

devkhan commented Jul 1, 2016

There's one issue, though. According to your snippet, getFiles is called before prepareFiles defeating the purpose of initializing member variables.

@jrmichael
Copy link
Contributor

Oh right.
We cannot call @BeforeClass blocks before parameters calculation. It is a part of test execution, while parameters calculation has to happen earlier in order to establish amount of tests and their description.

Sorry, but you will have to try a different approach.

@devkhan
Copy link
Author

devkhan commented Jul 1, 2016

I thought so. For now I'm just using indexes instead of actual objects and initializing them in @BeforeClass like previously.

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

No branches or pull requests

2 participants