How to run a JUnit test in eclipse? - 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.

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.

Disable import of JUnit 3 classes

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.

How to run a single test under FunSuite in Eclipse

I'm running unit tests extending org.scalatest.FunSuite using the ScalaJUnit Test (or plain JUnit Test) runner in Eclipse Kepler. When I select a single test to run in the JUnit pane, it runs, but then all the other tests run as well, under a heading designated "Unrooted Tests".
Is there a way to get just the single test I want to run? I hate having to comment-out all the other tests just to simplify the output and save time. Thanks!
It looks like this is possible when using the scalatest plugin for Eclipse (see this page: http://www.scalatest.org/user_guide/using_scalatest_with_eclipse ) and version 2.0 or later of ScalaTest itself. In my case I had already found and installed the plugin, but I needed to upgrade the version of ScalaTest.
I performed the upgrade by changing the scalatest line in build.sbt, then asking SBT to regenerate the Eclipse project. I hope there was a simpler way.

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.

How-to integrate gUnit ( ANTLR grammar testing ) in the NetBeans build cycle?

Background: I am using NetBeans 7.1 and ANTLR 3.4. I have integrated java code generation in the NetBeans build script using the following tutorial: http://wiki.netbeans.org/Integrating_ANTLR_without_learning_Ant. I want to use automated tests for testing grammars. I have read about gUnit on the ANTLR site. There are, supposedly, two modes: one 'direct' method and another method which generates jUnit code. I have worked with jUnit before.
Question(s):
What method is advisable? Direct or via jUnit? Or perhaps both?
What should I do to integrate gUnit in the NetBeans build cycle?
My personal preference is to use JUnit since then you can run the generated tests like any other JUnit test in NetBeans.
To get the JUnit classes you need to run the org.antlr.gunit.Interp class (in the Antlr JAR) as a Java program with the -o flag. The result will be JUnit classes in the same dir as the gunit file. Add the generated source to your project and compile/run.