Disable import of JUnit 3 classes - eclipse

I want to develop JUnit 4 tests only. When writing Unit Tests, Eclipse often imports classes from junit.framework, which is JUnit 3.
This has lead to various problems, e.g. when expecting an Exception, it simply doesn't catch it if it's in the wrong package like this:
import static org.junit.Assert.*;
import junit.framework.ComparisonFailure;
[...]
try
{
assertEquals(0, 1);
}
catch(ComparisonFailure cfe)
{
}
Strange enough, if I Ctrl+Click on ComparisonFailure, it says
Source not found
The JAR of this class belongs to container 'JUnit 4' [...]
Perhaps helpful environment information:
I don't have JUnit 3 in my build path.
Eclipse Luna 4.4.1
How can I stop Eclipse from importing JUnit 3 classes?
I have read Why is Eclipse using JUnit 3 when I have junit-4.3.1.jar in my build path?, but it's rather old and probably does not apply to Luna any more. Also, my problem is not in running the test, it's in implementing the test.

Another workaround for Eclipse's users is the following solution:
Windows -> Preferrences -> Java -> Appearance -> Type filters
and add junit.framework.* to the exclusion list.

Actually, JUnit 4 depends on some of the classes that were developed originally within JUnit 3 or reside in packages junit.*. One of such class is ComparisonFailure. If you look at latest JUnit 4.12 you will see that these packages are still there.
However, sources jar do contain java files for these classes. Perhaps your library that contains JUnit (do you use Eclipse JUnit library?) lacks source files for these? Where does your dependency (junit.jar) come from?

Which dependencies has your plugin ? Junit 3.X or 4.X ?
You could search your workspace for any references in junit 3 and change/remove them.

Related

test Grails 3.3.8 app with junit 4 or 5 in eclipse 2018-12, is it possible?

i m working on a grails project and i want to add tests to it.
I used spock and Geb but i prefer JUnit but it doesnt work.
i m creating a new Groovy Test with the eclipse wizard, i select either junit4 or junit 5
then it creates a new test class
i right click and select run as> Junit Test
it should show the junit view with failing test but no
i get a lot of "Invalid package binding for default import java.util/net/io"
and a "could not retrieve superclass","AbortCompilation ... AssertionError cannot be resolved. it is indirectly referenced from required .class file"
from org.eclispe.jdt.core
and i get an invocationTargetException caused by a NPE from org.eclispe.jdt.junit
those are my last errors from my many attempts, i feel like it s just impossible to do it.
if someone managed to make it work, pls tell how you did it
regards
test Grails 3.3.8 app with junit 4 or 5 in eclipse 2018-12, is it
possible?
Yes, it is. There are numerous ways to do that.
A common way to do that is to create a launch config in Eclipse that will execute the test or integrationTest Gradle task with the appropriate parameters.

How can you run junit5 tests across multiple projects in Eclipse?

I am looking at migrating the unit tests for a set of projects from JUnit4 to JUnit5. This is proving to be generally pretty straightforward. However there is one problem outstanding: how to run unit tests across all projects from within Eclipse.
With JUnit4 there is a simple solution using ClassPathSuite: create a new project which has all the other projects on it's classpath and add a single class with no methods:
#RunWith(ClasspathSuite.class)
public class RunAllTests {
}
This still works fine with tests written for JUnit4 and run with JUnit5 using the vintage engine. However once tests are converted to native JUnit5 ClassPathSuite no longer finds them.
Eclipse Oxygen (v4.9.0) Test configuration only allows tests to be configured within the confines of a single project, package or source folder so does not appear to offer a solution to this problem.
Any suggestions?
JUnit 5.8 introduced #Suite and suite engine.
This functionality seems to be similar to what ClasspathSuite JUnit 4 extension offers.
Eclipse seems to be working pretty well with JUnit 5 suites:
All the tests on the screenshot come from different projects.

EclEmma code coverage plugin and PowerMockRunner

I met a problem of using EclEmma plugin in Eclipse.
It doesn't show code coverage when I use annotation #RunWith passing (PowerMockRunner.class) as an argument. In case of using (MockitoJUnitRunner.class) everything is fine.
Waiting for your suggestions? :)
This is a known problem with Eclemma (JaCoCo). Basically both libraries modify byte code but Mockito modifies it during runtime which produces a different binary .class file. JaCoCo apparently uses keeps track of the classes it's watching via a hashMap whose hashcode is derived by the class definition so the hashcode will be different after Mockito modifies the class so JaCoco can't find the class in it's internal Maps anymore.
Discussed in more detail:
Why EclEmma doesn't coverage code with tests with #RunWith(PowerMockRunner.class)
Emma code coverage with JUnit and Powermock
http://code.google.com/p/powermock/issues/detail?id=402
Per this link last update, updating EclEmma plugin (by Uninstalling and Reinstalling) along with latest PowerMock and Mockito JARs in Eclipse (3.7) resolved the issue.
This change log confirms that.
Powermock works well with Eclemma 1.5.3; but doesn't work with latest Eclemma 3.0.0 !!
Eclipse - Kepler Service release 2
JUnit4
PowerMockito2 - 1.7.0.
Eclemma 1.5.3
The below given steps are working and I am able to see non-zero coverage % on running junit tests.
Download eclemma-1.5.3.zip from http://www.eclemma.org/download.html
Extract the contents to a local folder (eclemma-1.5.3)
Add this path to Menu bar : Window -> Preferences -> Install/Update -> Available Software Sites
Help -> Install New Software -> in "Work with" drop down, select the new entry (added in prev. step)
Uncheck the check boxes one after another then the "Eclemma Coverage" plugin options will be shown. Eclemma Coverage option will be shown in selection window.
Proceed to next and complete...
Enjoy !!!
EclEmma modifies bytecode to detect code coverage as PowerMock does to allow you to run an inaccessible code. Both changes are incompatible
The solution is to use a runner offered by PowerMock which workaround that incompatibility
http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/

How to run a JUnit test in eclipse?

I'm using eclipse and I have downloaded a JUnit test, and imported it into my package that I'm currently working on.
However instead of showing test.java it shows test.java.txt. And when I click 'Run as', JUnit test does not come up as one of the options.
Thank you in advance for your help.!
For the "Run as JUnit" option to appear, the file must be a Java file (extension .java).
JUnit scans your class for test methods. Depending on the version of JUnit that you're using, it can be done in multiple ways. JUnit before version 4.0 requires your test methods' names to start with the word test; with JUnit 4.0 onwards, you can use annotations to designate your test methods.
If your code contains JUnit annotations (such as #Test), but JUnit still complains that it can't find any test methods, then it means that you're running JUnit 3.x and not JUnit 4.x.

Using JUnit #Rule in Eclipse's JUnit runner

Eclipse 3.7.2
I just implemented an #Rule in some JUnit 4 tests, but when I run them in Eclipse the MethodRule methods are not being called. It's like the Eclipse test runner doesn't recognize the #Rule implementations and doesn't do anything special with fields that are annotated with #Rule.
I even tried using a "Standard" MethodRule like org.junit.rules.TestName but it doesn't work properly (the test's names are not populated into the instance). Even the example test in the JavaDoc of TestName fails when run in Eclipse.
Is there some trick? Does Eclipse simply not support JUnit Rules?
It turns out that somebody had included a seemingly "required" JAR on the runtime classpath of the project in question. That JAR embeds, among other things it should not, the JUnit packages! So there is this JAR, named something innocuous like our_runtime_library.jar that has some app-specific code along with some unknown (but old) versions of JUnit, Spring, and who knows what else. When running the project as a Unit Test, Eclipse was picking up the JUnit in that JAR instead of its own version (as it should, project-specific libraries always take precedence), the (Eclipse) version that the project is built against.
What a mess; now off to figure out who deserves 50 lashings for this one.
It should work, at least in my Eclipse(in embeded JUnit 4.8).
So you could show your code.
Additional, JavaDoc says: Note that MethodRule is now deprecated, you should be using TestRule instead.