Why can not add program argument to the junit configuration? Follow
Answered
I have created a test and want to run my test. I use the latest version of IntelliJ with spring boot 2 and spring test starter.
I should be able to pas the program argument to the application. when I set up a JUnit run configuration the program arguments field is gray and disabled.
because of this, my test fails.
Please sign in to leave a comment.
Hello,
The only supported scenario to create a configuration with an argument is from displayed parametrized test results run previously.
Here program parameters field is overused as one can’t pass parameters to the junit runner - only predefined parameters are allowed and I don’t think that it makes sense to allow editing of the field.
then how can I pass program argument which my application needs them?
If you want to pass parameters to the unit tests, consider using VM Options field, like
-Dparam=value
and in the test method you can read it withString value = System.getProperty("param");
When I debug the app deep down with running test, spring starts the main method of application which needs args which is empty while I have defined it in the run/debug configuration of the application.
JUnit tests are executed via the test runner framework, your
main
method is not called at all by JUnit, therefore you can't supply program arguments this way.To pass the arguments to the application via its
main
method you have to use Application Run/Debug configuration type in IDEA.I have already done this in the Application Run/Debug configuration but problem pops up when I run JUnit test.
As I said, in case it is needed to pass an argument to program via JUnit test, please use VM Options
Thanks a lot for your kindly reply, I'm gonna use it in this way as you mentioned.