Why can not add program argument to the junit configuration?

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.

0
8 comments

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.

0
Avatar
Permanently deleted user

then how can I pass program argument which my application needs them?

0

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 with String value = System.getProperty("param");

0
Avatar
Permanently deleted user

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.

0

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.

0
Avatar
Permanently deleted user

I have already done this in the Application Run/Debug configuration   but problem pops up when I run JUnit test.

0

As I said, in case it is needed to pass an argument to program via JUnit test, please use VM Options

0
Avatar
Permanently deleted user

Thanks a lot for your kindly reply, I'm gonna use it in this way as you mentioned.

0

Please sign in to leave a comment.