TestNG: Eclipse confused by JUnit - java

I'm setting up tests with TestNG in an Eclipse project, but I get a strange error: When I try to generate a test class from a business class, I get a popup with a message saying "Grid not editable" (title) "The compilation unit is not compilable, or is not a sublcass of junit.framework.TestCase. Fix and tyr again" (body).
Somehow, Eclipse seems to think I want to create JUnit classes, and I can't get why. Any clue?
Configuration:
Eclipse 3.6.0
TestNG 5.14.10

Google CodePro Analytix, the newly open-sourced static analysis tool, is the cause of the problem. It creates a new default editor filter which tries to open each class which name ends with "Test" with its specific JUnit editor. The TestNG plugin for Eclipse, on the other side, generates test classes using the tested class name, with "Test" added at the end. So when you create a test class with the TestNG plugin, it's opened by default with CodePro Junit editor, which dispays an error popup saying the class is not a valid JUnit test class.
Solution is to remove the editor filter in Eclipse Preferences:
Window -> Preferences ; General -> Editors -> File Associations
Select the *.java file type, pick Java Editor and click on the "Default" button.

Related

How to run single class ignoring compilation error in other class

I am using IntelliJ IDEA 2017.3. I have a project called "test", which has two classes under the src folder:
Main, has main method to print out "Hello World"
CompileErrorClass, has one method with compile errors
Those two class have no dependencies. I was trying to run the main() in the Main class, and set the before launch to "build, no error check"
I got an error message
"Error: Could not find or load main class Main".
When I fixed the compile error in CompileErrorClass, the main() worked as expected. Or if I ran the same project in Eclipse, it worked fine even with compile error.
How can I setup the run configurations in IntelliJ to run a single class ignoring the compile errors in other class without dependencies?
To be able to run code with errors you need to make a change in the settings. Go to Build, Execution, Deployment | Compiler | Java Compiler and set the Use compiler: combobox to Eclipse and enable the proceed on errors checkbox. This allows to compile classes even when they have errors.
To start a Run Configuration when the project has errors, you will need Build, no error check in the Before Launch section, instead of Build.
You can put your CompileErrorClass in a folder, then go to "Project Settings/Module" and mark the folder as "Excluded". The folder will be shown on red.
Shorcut: you can "right click" the folder an select "Mark Directory as Excluded".
In the image you can see the "bean" directory in red.
If you want to run the 'main()' method in the Main class and see the output without fixing the errors in the CompileErrorClass:
Select the 'Main' class in the project view, go-to Run --> Edit Configurations --> Before Launch and in Before Launch click + and select Build, no error check or click '-' take-out(delete) any setting in there --> Click Ok. If you take-out(using '-') any setting, then 'Before Launch' should not have any setting, its setting should be empty, null.
After the above, right-click on the Main class, and run it. You would see the output. You would see the output even if Use Compiler, which is at Settings --> Build, Execution, Deployment -->Compiler --> Java Compile --> Use Compiler, has javac selected as the compiler.
The steps are as follows:
Set Do not build before run (only need to set it once)
Menu → Run → Edit Configurations...
☑︎ Your run configuration
Run / Modify options → Java / ☑︎ Do not build before run
Recompile only selected files (remember to do this step before run)
Select packages or files that needs to be compiled.
Menu → Build → Recompile selected files (⇧ ⌘ F9)
Run or debug your run configuration.

How to get inspections or static code analysis errors in intellij Idea from Intellij Idea Plugin programmatically?

I want to create a plugin for intellij idea and in that I want to get the inspections or code analysis errors.By default intellij will show those code inspections like errors, dead code, or unused components.So I want to get those inspections pro grammatically to my plugin.I am able to create an tool button from my plugin and getting the code. Process is,
created plugin for intellij Idea with some actions and tools
menu.
running the plugin, created separate instace in
intellijIdea
wrote some java code in new instance of intellij in editor
-->in that it will show
inspections or errors
so I want get those inspection to my plugin.How can I do that?
The simplest API for getting the inspection errors in a given set of files is CodeSmellDetector:
CodeSmellDetector.getInstance(project).findCodeSmells(files);
If you know what exact inspection you need, you can get it this way:
PhpUnusedAliasInspection inspection = new PhpUnusedAliasInspection();
InspectionManager manager = InspectionManager.getInstance(psiFile.getProject());
List<ProblemDescriptor> checked = inspection.processFile(psiFile, manager);
System.out.println("checked " + checked);
Outputs:
checked [Import 'Illuminate\Database\Eloquent\Model' is never used]
Where PhpUnusedAliasInspection may be any class that extends the LocalInspectionTool (you can list them all by opening the decompilled LocalInspectionTool class and clicking the Subclasses circle near the name).

Selenium Java - how do I run only one test?

I am currently using Java and Selenium and I have over 50 tests in just one class. Is there an easier way to run only one test, other than putting #Ignore on every other test?
If you are using Eclipse you can do following:
go to your class file (source code)
right click on your testing method name
click "Run As.."
click "Run Junit Test"
it will run the one that you want to test

Change default "Program to Run" in Eclipse launch configuration

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.

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.

Categories