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.
Related
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.
Is there an option to define default JVM arguments in IntelliJ IDEA?
For example, I would like to always activate:
-ea -Dslf4j.detectLoggerNameMismatch=true
In Eclipse, the option can be found in:
Preferences -> Java -> Installed JREs -> Edit -> Default VM arguments
You have to do it per run type (Application, JUnit test, etc).
In the "edit configurations" window ("Run" ▸
"Edit Configurations..."), expand the "Defaults" item, select the appropriate run type, and edit its defaults in the left pane.
The full documentation is available in the IDEA docs.
You can set your VM options for all applications as a default by picking the application type (for command line Java programs it would be 'Application') under the 'Templates' tree found via the menu items Run/Edit Configurations... Then all new applications you create will use that default. Older versions of IntelliJ IDEA labeled that tree 'Defaults'.
This is the field in the Run/Debug Configurations dialog you need to edit:
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.
Is there a way to set System Properties which are used for every JVM that is started by NetBeans (NB 7.3.1 on Win 7)?
In all my maven projects I use Log4j wich needs a log4j.properties file, to where I want to point to by a System Property -Dlog4j.configuration=file:/c:/log4j/log4j.properties
I could set Global Execution Options for Maven: Tools > Options > Java > Maven > Global Execution Options. But then when I run a particular JUnit test class in NetBeans (thus without Maven), NetBeans won't use these JVM System Properties set in the Global Execution Options field.
Same issue is when a Main class in a Maven module is run in NetBeans. To set JVM System Properties there are Project Properties which could be set: 'Main Class' and 'VM Options'. But these Project Properties are only used by NetBeans when the 'Run' command is used from the context menu of the maven module. (Unfortunaly this menu item doesn't have a shortcut key (normaly Shift-F6)).
I have worked around this by setting a System Variable JAVA_TOOL_OPTIONS=-Dlog4j.configuration=file:/c:/log4j/log4j.properties.
Edit
A question was asked for the same problem I ran in to: How to make Netbeans use specific JVM parameters when running tests? In my case the problem was caused by the option Compile on Save which I had swiched on (File > Project Properties > Build > Compile > Compile On Save). In that case it seems that NetBeans doesn't use Maven. For further info about CoS: http://wiki.netbeans.org/FaqCompileOnSave
There's a file called project.properties in the nbproject folder. In this file there is a lot of configurations you can do that are not in the GUI menus. I don't know exactly what to put there to achieve what you want, but I found this on one of my NetBeans projects config:
# Space-separated list of JVM arguments used when running the project
# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
# or test-sys-prop.name=value to set system properties for unit tests):
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
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.