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.
Related
I am trying to setup my JavaFX enviroment so I referenced the corresponding Jars. In order to run the code I need to use some VM Arguments
("vmArgs": " --module-path /Users/adrif/Downloads/javafx-sdk-14.0.2.1/lib --add-modules javafx.controls,javafx.fxml")
in the launch.json. I am working in a multi-project folder so every time a make a new one I have to go to the launch.json and add the arguments. How do I set global arguments? I found adding arguments in "launch" at user settings.json (Launch - Global debug launch configuration. Should be used as an alternative to 'launch.json' that is shared across workspaces)
but doesn't work as I expected.
As you can see, I have two JavaFX projects under the package JavaFX.
And every time I run a new project I have to add the vmArg line to each project.
This is the "launch" options but as I said, it doesn't work or there's someting wrong with how I'm trying to do it. So as the title sais, how to I set global arguments?
Edit : I found the "java.jdt.ls.vmargs" option which allows to specify extre VM Arguments. I does not work either.
Solved, turns out it was fixed in newer versions of Vscode and it's pertinent Java Extensions. The workaround that I used was just adding a new set of arguments for every file in the project.
"vmArgs" was JVM related, "java.jdt.ls.vmargs" was java language server related, the language server was contained in the "Language Support for Java(TM) by Red Hat" extension, so it will not work.
The "launch" configuration in settings.json I tried in local and does not work too.
VSCode has not provided this feature for the present, You need to configure the "vmArgs" for each project. This is because the launch.json file contains different language debugging settings, and lots of configuration was exclusive to some language.
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.
While developing some plugins based on Eclipse, I need to add some default VM arguments when user right click-> "run as" -> "java application" or "spring boot app", is there any open api I can call or any extension point?
Just like the snapshot below, the argument "-Daaa=bbb" is added by default.
Any help is appreciated.
You need to write your own plugin and need to replicate/extend Java launch configuration
(of type Java application) from JDT and add -vm argument programmatically.
But there is one more easy way: Try LcDsl.
LcDsl provides a way of defining Eclipse launch configurations in a textual way. The file extension used is '.lc' It provides some obvious and some non-obvious advantages over the Eclipse launch configuration solution.
For more info look here and check demo videos. Also there was a discussion going on to include this plugin in JDT itself, see this bug entry.
A typical Java launch configuration looks like this:
java configuration LcJavaMain {
project com.wamas.test;
main-class com.wamas.test.JavaMain;
memory min=64m max=256m;
vm-argument '-Dmy.arg=value';
argument 'cmdArg';
}
every now and then I am launching JUnit tests from within Eclipse by using
Run As > JUnit Plug-in Test
By default Eclipse assumes you are running something which requires a workbench and chooses in the "Main" section of the launch configuration
LaunchConfig > Main > Program To Run > Run an application >
org.eclipse.ui.ide.workbench
I can understand why this is the default, but for me (and for all in our team) this is never ever the case. We always need to run our JUnit Plug-in Tests as
LaunchConfig > Main > Program To Run > Run an application >
[No Application] Headless Mode
How do I change this default behaviour?
I am currently using Eclipse 4.4.
It seems a custom LaunchConfiguration-Extension is a viable solution attempt.
What I did was to create a new, custom LaunchConfiguration-Extension which is 99.999% build on the JUnitLaunchConfiguration. I only had to add a custom
BlaBlaJUnitPluginTestLauncher extends launching.JUnitLaunchConfigurationDelegate
which overrides the
launch(ILaunchConfiguration, String, ILaunch, IProgressMonitor)
method to adjust the application parameters according to our needs.
BlaBlaJUnitPluginTestTabGroup extends org.eclipse.pde.ui.launcher.JUnitTabGroup
To be able to initialize the LaunchConfig dialog with the default parameter, I had to:
Add a custom BlaBlaPluginJUnitMainTab extends PluginJUnitMainTab
Create a custom JUnitProgramBlock implementation (BlaBlaJUnitProgramBlock)
Creating an instance of BlaBlaJUnitProgramBlock in the BlaBlaJUnitPluginTestTabGroup.BlaBlaPluginJUnitMainTab.createProgramBlock() method
Overriding setDefaults-method (not sure if its really neccessary) in BlaBlaJUnitProgramBlock
Overriding initializeForm-method in the BlaBlaJUnitProgramBlock and adjust parameter there too.
Leading to the following result:
This answer is a near miss:
Try this:
Manually create and configure one "good" launch configuration.
Next time you want to launch a test that doesn't yet have a good launch configuration:
Select the file and invoke Run As > Run Configurations ... (i.e., don't yet select JUnit Plug-in Test!)
In that dialog select a good launch configuration of the same kind, and ...
Then click New Launch Configuration (upper-left corner)
Now the newly created configuration should "inherit" the configured values from the the good configuration.
Truth is:
You can duplicate an existing launch configuration (leaving you to manually select the test to launch)
The Debug team once had plans to support launch configuration templates.
Edit 2018:
Since Eclipse Photon, the Java debugger supports launch configuration prototypes. I just filed Bug 536728 to request this also for test launches. Feel free to chime in (or contribute) on that bug.
if u r looking for only shortcut for convenience then eclipse remembers last execution. After using run as and saving ur run config, just use "Run as" button in toolbar.
besides this eclipse comes with flavour for testers, u can check that out.
Also since you are talking about unit testing see if you can make use of ant build or even better converting to maven based project. Maven has integrated support for testing.
There seems to be a simple and effective heuristic in place, which decides whether or not a JUnit Plug-in Test should be run headlessly or with an application:
Make sure that the plug-in containing your tests has no dependencies on anything org.eclipse.ui.
Without that dependency [No Application - Headless Mode] is selected by default for newly created launch configurations.
With that dependency the default is Run a product, with s.t. like org.eclipse.platform.ide preselected.
When I implement new features using TDD, I often use the shortcut Shift + Alt + D T to run only the jUnit test case I am currently working on (and not the whole test suite, which takes a few minutes).
This creates a new Debug Configuration for the current Java source and runs it immediately. Now I would like to run the test with assertions enabled (VM option -ea).
Unfortunately, the VM arguments for the new debug configuration starts empty. I always have to go into Debug Configurations... → Arguments and add -ea to the VM arguments input box.
Is there any way to have Eclipse these arguments populated with (project or workspace wide) default options?
I think you can set this option by editing the definition of the JRE under Preferences → Java → Installed JREs → Select the JRE in question and edit the default VM arguments
Windows → Preferences → JUnit has an option to add -ea every time a new launch configuration is created. It adds the -ea option to the Debug Configuration as well.
The full text next to a check box is:
Add -ea to VM arguments when creating a new JUnit launch configuration
Only way I can see to do it is to contribute a new launch configuration, this is not a trivial task but not too hard if you have PDE experience.
There is an eclipse.org article (see "Creating a Launch Configuration") that describes how to construct configurations and launch them.
I guess you could copy the JUnit implementation (see the org.eclipse.debug.core.launchConfigurationTypes extension in org.eclipse.jdt.junit) and add in your chosen defaults to that config. You'd then need to declare a key binding to launch it and package the plugin to your install.