Using JUnit #Rule in Eclipse's JUnit runner - eclipse

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.

Related

Eclipse junit tests using wrong version of maven jars

I have two projects that I compile and run together. These projects use different versions of certain jars. so I may have foo.1.15.jar and foo.1.16.jar both in my maven repo. The pom files correctly specify which project should use which jar, and I can run mvn verify to run all my integration tests without issue from the command line.
However, when I try to run a junit test from within eclipse I often run into errors with the wrong version of the jar being used. I believe that eclipse is always using the newest version of a jar that it finds within the maven repo, so if it sees foo.1.15 and foo.1.16 it will use the 16 version. The problem is that I want the 15 version, the tests fail if they run with the 16 version because it's not backwards compatible.
How do I make eclipse recognize that it needs to use an older version? In particular is there a way that I can make the junit tests recognize my pom file and utilize the correct jar versions specified within the pom?
I'm using m2eclipse if that is relevant.
edit: it looks like the problem is not an insistence on using the newest jar each time.
The problem is that my project A uses my project B. A uses foo.16 and B uses foo.15. the junit tests for A tries to use foo.16 even when it's running classes in B that are dependent on 15. However, I can't really change the dependencies, A needs 16 and B needs 15. Can I make eclipse understand this?
Your assumption about Eclipse always using the latest version of a Maven artifact is not correct.
However, Eclipse only supports one classpath per project and cannot distinguish the Maven dependency scopes, so test scope dependencies are also on the Eclipse compile and runtime classpath. It's hard to tell if this is really related to your problem.
You can open the POM editor and look at the Dependency Hierarchy tab to find out the (transitive) dependency path and the version used for each artifact. This may given you a clue for fixing your setup.

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 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.

How Can I Make and Run GWTTestCase Based Tests with Intellij 12?

I've tried multiple times over a few months now to do this, but I haven't had any luck. I've found Google's documentation about how to setup GWTTestCase tests via the command line and Eclipse, but nothing about how to do it with IntelliJ. When I try to excite GWTTestCase tests in IntelliJ like a normal J-unit test, I get a bunch of errors. I assume that the Google Eclipse plugin is probably doing some sort of magic to make the tests work in Eclipse, so I'm not even sure if what I want to do is possible.
GWTTestCases are especial junit tests, because they have to execute some extra stuff like the gwt compiler. This is done automatically, but the compiler needs to know where your .java files are (main and tests). So previously to run the test you have to add the src, test folders (if you use ant) or src/main/java, src/test/java (if your project is a maven one) to the class-path of your launcher.
If you are using the native GWT support in intellij (it was previously called GWT Studio plugin), it should add those folders to the classpath. You can find more info in this page, although it is a bit outdated.

Setting up a Scala project in Eclipse, together with JUnit & Scalatest

I have recently completed the Scala course on Coursera, and since then I have been looking forward to getting my hands dirty with Scala again. I have written code for some years but I neither educated to be nor work as a programmer, so it took me a while to get a good opportunity but now that I have some time to invest and a good project to work on it's time...
Except I can't seem to get things set up properly, which I find really frustrating. I have OpenJDK 1.7.0_25 running on my Linux machine. I have downloaded and installed the Bundle Scala IDE build for Eclipse (just like we used in the course). And I got ScalaTest both as a jar file and the Eclipse plug-in.
I have a simple project (so far) and no matter what I do I can't seem to get my builds and tests in order. First off how exactly am I supposed to set up my project so that my classes and tests are actually run properly? All the assignments we got were projects that had the same structure, so do I have to have:
project
|--src
|--main
|--scala
|--test
|--scala
structure? If so why is it not the default way the project is setup when I create a new project? Do I create these folders manually, as packages or as source folders? The whole thing gets pretty murky..
I should mention that I tried to "Mavenize" the project using the contextual menu in Eclipse, added my ScalaTest dependency. The first thing that happens is that I get compile errors, at every point of dependency in my code. So clearly the library is not visible, in other words Maven does not seem to be doing much of management. I thought the whole point of Maven was to get and maintain dependencies as the project evolves. I concluded that I do not fully understand the way Maven works and thus I eventually gave up on Maven, once again, and went back to doing things manually.
Secondly, I can't seem to run my tests; the Run As... menu item does not include ScalaTest as it's mentioned in the documentation of ScalaTest Eclipse Plug-in. I have double checked that the plugin is installed. If I instead try to run using JUnitRunner then my tests are not recognized as valid tests. I have JUnit and ScalaTest on my build path, so it's got to be something else.
I suppose my overarching question is as follows:
given the Scala IDE build of Eclipse and ScalaTest, just exactly how am I supposed to set up my project (in Eclipse) so that I can just focus on writing my code and testing it, and hopefully not have any other headaches?
I work alone, and this project is not a product I need to deliver to some client. In other words I do not need to adhere to strict professionalism here. Honestly I just want to be able to code, get better acquainted with Scala and hopefully build a small data analysis tool that I will be using from time to time.
Thanks in advance!
Try using the sbt eclipse plugin:
https://github.com/typesafehub/sbteclipse
This is of course assumes that you use sbt as you build tool. If you don't at the moment you can find instructions on installation and usage here: http://www.scala-sbt.org/
Personally I've been using typesafe giter8 template (https://github.com/typesafehub/scala-sbt.g8) to setup my Scala projects, and then I use the sbt plugin mentioned above to generate eclipse project files.
Scala is somewhat Maven-based (sometimes implicitly), that's why you use that structure.
The easiest way I think is to create a simple Sbt/Maven POM and create the Eclipse project configurations (like with sbt eclipse). There you can set the dependencies (like the actual version of JUnit, Scalatest to use), so you can use the ScalaTest plugin easily.
In case of other issues, feel free to ask at the ScalaTest mailing list, Chee Seng and Bill Venners can help you a lot there.
The Scala IDE website has a full documentation on how to run unit testing frameworks with the IDE, have a look ! If you find missing elements, the bug tracker of the scala-IDE project is here.