Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG: Eclipse confused by JUnit

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
like image 522
Alexis Dufrenoy Avatar asked Feb 28 '11 15:02

Alexis Dufrenoy


People also ask

Can TestNG run JUnit tests?

You can execute your existing JUnit test cases using TestNG. TestNG can automatically recognize and run JUnit tests, so that you can use TestNG as a runner for all your existing tests and write new tests using TestNG.

Can you convert JUnit test scripts to TestNG?

All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, change your test runner from JUnit to TestNG in Ant and then run TestNG in "mixed" mode. This way you can have all your tests in the same project, even in the same package, and start using TestNG.

Does Eclipse have JUnit?

JUnit simplifies the Unit testing process in Java with visual indicators and a Unit testing framework. JUnit use in the Eclipse IDE is convenient and quick to configure.

Why is JUnit not working in Eclipse?

The error occurs because the JUnit library has not been configured for the project, but can be resolved using the following steps. 1. Right click on the Java project and select Build Path > Configure Build Path. 2.


1 Answers

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.

like image 139
Alexis Dufrenoy Avatar answered Oct 11 '22 12:10

Alexis Dufrenoy