How to setup Groovy + Eclipse + Junit4? - eclipse

I am working on a small webapp and I want to use Groovy to write some unit testing for my app. Most of my coding is done on Eclipse and I really want to run all the unit testing with the graphical test runner within Eclipse (I really like the green bar :) )
Sadly, after 4 hours of try-and-error, I'm still not able to setup properly. I tried to use the Eclipse Junit4 test runner to run a Groovy file with method annotated for testing using #Test. But it keeps complaining NoClassDefFoundException
Anyone can help?
Here is content of my groovy file, named simpleTest.groovy
import org.junit.Test
import static org.junit.Assert.assertEquals
class simpleTest{
#Test
void trial(){
assertEquals 6, 3+3
}
}
Anyone can help?

You might want to give the updated plugin a try, see the the recent blog post for more details. The theme of this alpha release is providing an optimized edit/save/compile/test experience, which seems to be your exact use case.

I have this working in my environment so here is a brief summary of what I have:
In the run dialog under JUnit:
Test Tab: The test class, this must have already been compiled by the Groovy plugin.
Classpath: All of the Jar files from my project as well as the Groovy Libraries library
In Window->Preferences->Java->Build Path
Classpath Variables: GROOVY_ECLIPSE_HOME = the location where the Groovy plugin is installed
That does the trick for me.

Unfortunately, the Groovy Eclipse plugin is pretty horrible at giving actual helpful information to let you know what is going wrong with your setup. I'm going to assume you already did the verification to make sure the plugin is actually building your Groovy files (i.e. doing a sample with no dependencies, checking the properly output directory, etc...) After that, it's a lot of really small configuration verification...I've run into problems where the particular "runner" I'm using in Eclipse (i.e. in the Run menu) doesn't have the write class name defined there or for some reason my project didn't get the JUnit library dependency properly inserted into it.
Ultimately, it can be a configuration headache, but long term you'll end up saving some time and gaining some cool functionality if you can knock it out...

I had faced a similar issue and it was the missing package statement that caused me to have problems. Groovy Eclipse plugin did not complain about it but my class was present in a package. I got the noClassDefError when running the file as a JUnit Test.
Adding the package statement to top of class solved this issue.

Related

How to solve "Package org is not accessible" error in eclipse while importing org.junit.Assert.*?

I am following a tutorial on unit testing from school and wrote JUnit tests for a method in a class. This is what the files look like : enter image description here
Why am I getting this error? All the annotations like #AfterClass are also underlined with error "cannot resolve #AfterClass to a type" How do I fix this ? I have programmed in Java for a year now but this is my first time using eclipse and writing JUnit test cases.
The JUnit classes are in a library, which is not automatically made available to your project, unless you make it available. For a simple exercise like this, you might have to download the JUnit jar file (https://github.com/junit-team/junit4/wiki/Download-and-Install) , and create a "lib" directory in your project to store it. Then, you add that jar to your project classpath.
If you've done everything right up until this point and are still having issues with your junit imports, your module-info.java file is probably the culprit. If you don't need it, deleting it will silence the IDE complaints.

Can not import JUnit with Eclipse

Evnironments
Mac OS High Serial 10.13.6
Version: 2018-09 (4.9.0)
Hi,
I am having trouble create Junit Test Case with Eclipse.
I've google this error, but all the posts tell me that you have to add.
As shown below I had.
The error is The import org cannnot be resloved
Somebody know what are the possibilities of my problems ?
With #nitind help, I could solve the problem.
Because I did not know what is module path and google it.
I ended up reading this post and in that post it says
There is one special case: If you have a module-info.java in your
project and have test code in your project, you usually don't want to
mention test dependencies like junit in the module-info.java. There
are two solutions for this:
Create a dedicated test module. This has always been the convention
for osgi-based projects. Disadvantage is that you can only use public
api in your tests
The solution used by maven: Put your test dependencies on the
classpath. When compiling test code, maven adds command line options
that allow the code in the named module to read the unnamed module
(which is not possible via the module-info.java).
So, as it says I made dedicated test module like the picture below.
Right click the test module
Go Build Path > Configure Build Path
Then toggle Contains test sources: No to Yes
Check Allow output folders for source folders
Change the Output Folder to test (or anywhere you want other than you default (bin))
Run the test and the test should work from now.

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.

ScalaTest Run Configuration in Eclipse: cannot find Suite Class

Problem: I cannot setup Run Configurations to run scalatest for the Scalatests in my project.
Steps to reproduce:
Right click on Scala Suite and click on Run as -> Run configurations..
On the left, I see a configuration template for ScalaTest. I click on New and fill the Name but it cannot find the suite-class.
Note: It is mentioned here that I should see Run as -> ScalaTest - Suite but I do not see that option. I tried using context menu in the editor, and in the package explorer
Steps taken:
Using: Scala IDE for Eclipse version: 2.1.0.m3-2_09
Using SBT, assemble project, run eclipse command and then import project and dependencies into Eclipse
Project compiles. ScalaTest code compiles(scalatest_2.9.2-1.8.jar is in the 'Referenced Libraries' configuration)
I've been fighting a similar problem for the past few days; Lily / Jimbo's answer didn't quite match my situation, but helped me find the right direction.
In my case, I was using a third-party library that I'd copied in. The package names of the classes and tests matched, but the folder structure did not -- all of my tests were directly in Play's "/test" folder, rather than in folders that matched the package names. This didn't show any errors, but was broken: packages ought to match folders. When I built the right folder structure underneath test, and recompiled, the expected "Run As -> ScalaTest - Suite" options showed up.
Don't know if your problem is the same, but you might check this if you haven't already found the issue...
This could be caused by a misalignment between the scalatest and the scala eclipse IDE version. Try scalatest_2.9.0-2.0.M5b.jar or scalatest_2.10-2.0.M5b.jar. The former jar definitely works with ide 2.0.9.x so maybe the new version needs the 2.10 jar. Pick your version carefully from here
Willem's answer is what worked for me. Getting both plugins from the same update site (from the list on Scalatest's github site), seemed to work for me using Kepler.
for my case, one click on 'Reimport All Maven Projects' icon, like 'Refresh' icon, solved the problem.
Not sure if you fixed the error, but I had similar a error yesterday and was pulling my hair trying to fix it (none of the suggestions I found by googling seemed to help me). So for me, it turns out that it's as easy as package hierarchy in my test suite.
I am using the play framework, so naturally my folders look like this controllers.package1.package2.... and this applies to my test folders also.
Now my test classes however, have the package definition package1.package2..... (no "controllers" as prefix).
If I run the tests on sbt/play command prompt, it's not a problem. But running them through Eclipse would give me the problem you described.
So anyways... thought I'd share this, in case this could help.
it's a bit annoying combined with the view template compile issue in play framework. but my approach is to regenerate the eclipse project file and add view template path into the class path

How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

I recently gave up trying to use Scala in Eclipse (basic stuff like completion doesn't work). So now I'm trying IntelliJ. I'm not getting very far.
I've been able to edit programs (within syntax highlighting and completion... yay!). But I'm unable to run even the simplest "Hello World". This was the original error:
Scala signature Predef has wrong version
Expected 5.0
found: 4.1 in .... scala-library.jar
But that was yesterday with IDEA 9.0.1. See below...
UPDATE
Today I uninstalled IntelliJ 9.0.1, and installed 9.0.2 Early Availability, with the 4/14 stable version of the Scala plug-in.
Then I setup a project from scratch through the wizards:
new project from scratch
JDK is 1.6.u20
accept the default (project) instead of global / module
accept the download of Scala 2.8.0beta1 into project's lib folder
Created a new class:
object hello {
def main(args: Array[String]) {
println("hello: " + args);
}
}
For my efforts, I now have a brand-new error :)
Here it is:
Scalac internal error: class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:202), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java:190), java.lang.ClassLoader.loadClass(ClassLoader.java:307), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301), java.lang.ClassLoader.loadClass(ClassLoader.java:248), java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72)]
FINAL UPDATE
I uninstalled 9.0.2 EA and reinstalled 9.0.1, but this time went with the 2.7.3 version of Scala rather than the default 2.7.6, because 2.7.3 is the one shown in the screen-shots at the IntelliJ website (I guess the screen-shots prove that they actually tested this version!). Now everything works!!!
I have encountered the same scalac error when trying to run a Scala project in Intellij Idea 9.0.2 and I've managed to find a solution by chance :). These are the steps I took in creating the project and running it.
I have created a Scala project in Intellij Idea 9.0.2 final (it was released today). I have installed the Scala plugin, restarted the IDE and created a new Scala project (with the name "TestScala") with scala-2.8.0.Beta1 as project library. Once the project is created and the scala libraries downloaded, I have created a Test.scala file with the following content:
object Test {
def main(args:Array[String]){
println("hello")
}
}
After that, I created a launch configuration ("Edit Configurations"), choosing the "Application" template. I set as main class Test and choose the project name ("TestScala") in the "Use classpath and JDK of module" combo box. When I run the configuration I get the same error as you reported ("Scalac internal error: class java.lang.ClassNotFoundException") .
Now comes the freaky part :). I right click on the project, choose "Module Settings", have a look on all settings but I don't change anything . Click "apply" and "ok", try to run configuration again and it works :) .
I use Intellij Idea 9.0.2 the final release (build 95-66); Ubuntu 9.10 and JDK 1.6.0_18. I also have to mention that I had a JDK configured in Intellij, otherwise there is an extra step to configure it.
UPDATE:
When checking the setting of the module, one needs to click on the Module->Scala and Facets->Scala (expand it and click on Scala(ProjectName)) . Both of these settings are about the scala compiler and scala library location. I would guess these values are not properly set when the project is created but are saved once the user touches them and saves the settings.
To answer your question, it's difficult to get a working IDE for Scala for two reasons:
(a) Scala is only just beginning to reach a wide audience and
(b) due to (a), there is no business case for spending time on a Scala IDE.
Also, if you are old enough to cast your mind back and young enough to still remember, you would know that for the first five or more years of Java, we were stuck with okay-ish tools like JBuilder that did little more than compile your code when you said so - no error highlighting, no auto-importing, and the word refactoring didn't even exist. If you want to pioneer, you need to be prepared to cut some of the road yourself, or at least bush-bash.
I know it won't help you, but I have successfully used IDEA for Scala on Linux, Mac and Windows. I typically have the Scala SDK installed somewhere locally and point IDEA at that rather than using the 'download' option.
Presently, I am mostly using an EAP version of IDEA 9 on Mac OS X with Scala 2.8.0.Beta1-RC5 and it's working well (except that fsc doesn't seem to worked with mixed sources).
You could try your luck over at the IDEA Scala Plugin Discussion Forum, though I haven't had a great lot of responses to my own postings there.
Installing the plug-in is prerequisite one.
The next thing you should do is define a library (global or project-specific; I use global) that holds the Scala library and compiler JAR files (at a minimum, that's scala-compiler.jar and scala-library.jar). Adding source JARs and a documentation JAR or URLs is a good idea, too. Then make this library a dependency of any modules in your project that include Scala code.
Lastly, find the Scala facets in those modules and de-select both check-boxes there.
I just did a fresh install and had exactly this same problem myself.
It turned out that, because I had created the file in the root package, IDEA had added a package statement at the top with naming a package. I assume that this then got compiled as "package object Main" - valid syntax in 2.8? Anyway, I deleted the line that said package and it all worked fine.
I had the same problem yesterday while trying to set it up. Solution is pretty simple, you just have to set scala somewhere in project settings.
You are mixing code compiled with two different Scala versions.
I use Netbeans to write scala programs. So far it works very well with my codes. You can try the plugin here: http://wiki.netbeans.org/Scala68v1.
I was getting this error and also had to right click on the project and "Open Module Settings". However, it was more than just hitting apply. I had to make sure that my Content Root was correct for each project. For some reason, there were some incorrect Source and Test Folders.
My project uses maven as the main build tool and importing the project into Intellij is probably what created these incorrect settings.
I had similar problem, following this blog post instructions solved the problem for me