Sharing environment variables in IntelliJ JUnit run configurations - java

I'm using IntelliJ IDEA to write and run e2e tests with JUnit. My tests require some environment variables to be set, so I set these in the Run Configurations dialog (through Run-->Edit Configurations, Environment Variables field).
This works fine, but the problem is whenever I run a single test (e.g. by clicking the green "play" icon to the left of the code editor), it creates a new run configuration for me to run this single test without the environment variables I defined, which obviously causes it to fail. I need to open the Run Configurations dialog again and copy the environment variables from another configuration to the new one. Note that I need to do it for every single test that I run, which is very annoying.
Is there a way to set the environment variables only once and use them for every test that I run?

You can expad the "Defaults" in the list to the left, pick JUnit and then set the env variables there. This config will then get copied to whatever new configurations are created afterwards:

Related

Spring Boot + Gradle: take VM options when I run single tests

When I run Gradle tasks like test from IntelliJ, I set VM options and/or environment variables for all tests in "Edit configuration" dialog and they work. See my question at how to set spring.config.location for a gradle spring boot project in Intellij IDEA community version?
Now, if I want to run a single test, I must:
run test, which fails of course
edit configuration to add VM options and/or env vars
run it again
Is there some way to configure the VM options and/or env vars in build.gradle so that would be picked up by every single test automatically?
PS: the args are -Dspring.profiles.active=... and -Dspring.config.additional-location.
Try setting the system properties in the file gradle.properties like so:
systemProp.spring.profiles.active=profileName
spring.config.additional-location=additionalLocation
See also: https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_system_properties
Thanks #tarts, but I think I have found one thing maybe better: setting a template of run configuration in IDEA. The question may be reworded into: "can I in some way, not limited in build.gradle, to set these variables?"
In "Run Configuration", on the left side, there is a category named "Template" listing all possible categories of tests we can run and providing template for each, where we set universal values for single tests.
You can see that "Gradle" and "JUnit" and other many categories are listed, so we can change VM options here, then all unit tests are run with this param set.

How to setup default IGNITE_HOME environment variable in intellij

I want to setup default IGNITE_HOME = /home/arjun/Desktop/apache-ignite-2.7.0-bin environmental variable to my whole project test classes instead of setting every time for new classes.
How can i setup?
Got to Run -> Edit Configurations -> Defaults, choose a default you use during testing (e.g. Application for regular Java programs or JUnit for JUnit tests) and change the environment variables there. From now on the new configurations you add (e.g. when start a new test) will have these settings.
Note that the old configurations (e.g. the programs you've already run) won't be affected. You need to delete the existing configurations so that they're recreated with new defaults. Choose each configuration that is not in Defaults and hit del or the red minus sign.
Run - Edit Configurations - 选中 Application 下需要配置的项 - 右侧 Configuration 选项 - 配置 Environment variables - 添加环境变量即可。
Run - Edit Configurations - xxx - Right:Configuration - Environment variables
enter image description here

Java not finding environment variables defined in Jenkins

I have a snippet of code that is obtaining an environment variable as follows: System.getenv("MY_VAL")
locally on my windows machine this works fine.
However, on my Jenkins CI Server which is running CentOS I am encountering some issues
I have tried setting the value of MY_VAL through both the envinject plugin as well as the global jenkins settings
If i do a pre-build step to echo the value out, it works fine, however inside my java code this is not being resolved.
How do I get this to be resolved?
You can achieve that by installing EnvInject plugin.
1) After installing check the Prepare an environment for the job option in the job configuration screen. This option will display several field for you to fill.
UPDATE
2) Fill the Script Content area with a command touch env.properties to create the file.
3) Fill the Properties Contentt field with the variables you want to inject inside your recently created env.properties file by doing so. Place one variable per line ex:
VARIABLE1=value
VARIABLE2=value
4) Reference env.properties file you've just created in the file path area.
5) At runtime Jenkins will inject those variables and they will be available to your program.
You need to inject this variable into property.file and then access this from property file. e.g. in execute shell you can define "echo MY_VAL=default > property.file" .. Later in subsequent jobs you can pass through using "Jenkins Parameterized Trigger plugin" where you have option to access parameters from property file.

How do I set the main Java type in Eclipse?

How do I set the main Java type in Eclipse (latest)?
I have a main client, and then a bunch of unit tests that exercise various code paths and conditions. Normally I'd like to debug or execute the unit tests depending on the feature I'm developing. However, when the boss shows up, I'd like to quickly run my main driver class without hunting and pecking for the .java file, then hitting debug.
Is there an easy way to set/change the main type? How do folks normally handle running unit tests in Eclipse?
Click on the arrow in the run/debug icon and select Organize Favorites at the bottom and set favorites for anything you don't want to fumble around for later.
For unit tests, I just run them like anything else in Eclipse. I also include a test target in my Ant scripts.
For unit tests :
You can make test suite so to bundle them by feature. This way you just need to run the test suite related to your feature.
Having several launch configuration :
Just go Run Configuration... or Debug Configuration ... from Run menu and create the configuration you want. Next time you'll want to Run them go again to that menu select it and click Run/Debug;
To gain time you can go to Keys configuration and set at shortcut to "Run..." or "Debug..." submenu.
Right click on your test folder and "Run as ... -> JUnit test"?
From Run > Run Configuration... Create a new Run Configuration for the type of java application you have and the options let you specify the main class.
And these configurations will be available in the Run As button dropdown.

Defaults for Eclipse run configurations

I'm writing a Java library with a lot of jni code. Pretty much every test case needs to load my jni dll, and I have a lot of test cases. In order to run the test cases out of Eclipse's Junit launcher, I have to create a run/debug configuration and edit the VM arguments and environment variables.
I would like a way to set the VM arguments and environment variables to a default for the entire project and have new run configurations include the default entries. From what I can tell, Execution Environments maybe do something like this but I seem to need the PDE to get them to work(?)
Specifically, I want to enable assertions on my project by default and include the path to my native dll in the PATH environment variable. I can't use the "Default VM Arguments" setting in the JRE definition panel because my dll depends on a number of others and java.library.path isn't used for dependency resolution, PATH is. Is there a way to make Eclipse do what I want?
So, here's what I did.
First, my specific problem was that I have a lot of run configurations, I create new ones on the fly, and I needed certain system properties set for unit tests. Setting them under the 'args' tab of run configurations was undesirable for my workflow. Also, I wanted the same command-line args set for all of my tests. I also don't run my app from inside eclipse. It's a dev-environment only.
So my solution was to add it to the command-line of my JRE. Preferences -> Java -> Installed JREs. Clicking edit gives you a window where you can specify default VM args. I just set the system properties I need for testing there.
Hope this helps.
How long does it take to run all of your tests for the project?
If the answer is Not long then create a project-wide JUnit launcher. If occasionally you would need to do a run on a single test case ( in order to debug or something ), you can copy all your settings from the project's junit launcher. I think you can even clone your project launcher to run a specific test case.
Run->Run Configurations...
Create new JUnit launcher.
On 'Test' tab select Run all tests
in selected {...}
Connfigure JVM options, classpath,
environment etc. for this launcher
Optional, but highly recommended. On
Common tab -> Save as -> Shared
file, and check-in launcher with
your project
One more thing I would do is to define a system property in launcher VM arguments, check for this property in #Before function and throw exception if the property is not set. This way you will know that your test fails because it is not using the right launcher.
If I understand your question correctly, I think Alexander is on to the idea with cloning the project launcher. Eclipse lets you duplicate launch configurations with a single click - simply setup one configuration with the parameters you require and click the button in the top left to duplicate it whenever you create a new one.

Categories