Eclipse JUnit runner can't find ScalaTest methods on double click - eclipse

I'm taking the Martin Odersky Coursera class and the assignments use ScalaTest. It's quite annoying that Eclipse can't locate the test methods on double click or right-click "Go to File" in the JUnit runner.
Upon double click, a "Method 'xxx' not found. Opening the test class." dialog pops up.
Is there a configuration problem or is this a ScalaTest bug/limitation?
Here's a sample ScalaTest from the class:
package recfun
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
#RunWith(classOf[JUnitRunner])
class CountChangeSuite extends FunSuite {
import Main.countChange
test("manual") {
assert(countChange(4,List(1,2)) === 3)
}
}

You can use the ScalaTest Eclipse plugin with ScalaTest 1.8. But when you run your tests it will pop up a GUI. Useful, but not as nice as the ScalaTest 2.0 integration. I think with the Coursera course you are using 1.8.
The actual problem you're having with JUnit is one that we have tried to solve in ScalaTest. JUnit is not only a test framework, it is also a testing platform that you can run other frameworks through via its #RunWith annotation. In this case you're running ScalaTest through JUnit. But you don't get full IDE support with a #RunWith annotation, you just get the ability to run. This is indeed a limitation of JUnit. What you would like is to get IDE support too for that test framework you're running through JUnit (in this case ScalaTest). The only way to do that with JUnit is to write IDE plugins for your test framework, which is a lot of work.
We have been working on that as part of the ScalaTest project, making plugins or helping make plugins that give ScalaTest users full IDE support. But as part of that effort we also came up with the concept of a "Finder" that can be used to give other test frameworks full IDE support when they are run through ScalaTest. (I.e., just as you can run other test frameworks through JUnit, you can also run other test frameworks through ScalaTest.) You can see some examples of Finders giving IDE support at the end of this video (about 10 minutes into the video):
http://skillsmatter.com/podcast/scala/scalatest-scalamock-subcut

Yes, it's a known bug in the JUnit plugin for Eclipse. It's also very hard to solve (for the jun it plugin).
Basically, The Eclipse junit plugin expects there to be a method, which there will be for all java JUnit tests, but this isn't necessarily the case for ScalaTest tests, especially Suites & Specs.
However, you can try out the Scalatest plugin for Eclipse, which should work.

This works for me:
1- Open a console in your folder project and execute "sbt".
2- (without get out from"sbt") execute "eclipse" command.

Related

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 to run a ScalaTest form a different project in Eclipse?

I have two Scala projects open in Eclipse (the Scala IDE, more precisely). One project is the main project and the other is where I want to place my tests.
If I place a test file (i.e. a class Test1 extends FlatSpec) in my main project, I can right click on the file and choose "Run Configurations", and it offers me to "Run as ScalaTest - File". However, if I copy that same test file to the second project, right clicking doesn't offer that option. Why is that?
If I am not explaining the situation with enough detail, I'll be glad to add more information.
From ScalaTest guide:
Using ScalaTest in a Scala project
To use ScalaTest in your Scala
project, you must download ScalaTest and include it in build path of
your project.
If it is in your build path, you could just create run-configurations for that particular class.
I've had problems in the past with eclipse and not seeing run java application. I just created the run configurations myself.

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.