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

Related

what is the right way of importing a maven project in Eclipse and run it as a Java project?

I don't understand exactly what is the way of working with maven projects in Eclipse.
The problems I have are often with projects I download from github. If I set the project myself it usually work, so I think I'm doing something different from the majority of people.
I'll try to detail a specific case:
I clone a repo, let's say: https://github.com/spring-guides/tut-spring-boot-oauth2
In Eclipse I import "existing maven project"
The project has a "Maven nature" as indicated by M on the folder icon
I try to run java class with main from Eclipse. First strange thins is that the "Run As" menu doesn't have "Run as a Java application"
I have to configure the configuration manually. Now it runs, but strange things happens, like I can't edit the file as the "content assist" throw errors instead of giving the normal assists.
I notice that the project has no "source folder". So my first instinct is to add a Java nature or select src as source folder
So I add Java nature to the project. This is a disaster. It can compile anymore as it can find packages. All classes have errors. I try to play around setting source folders on /src or /src/main/java. Sometimes I fix the errors but I can't run (and now I have run as Java application) but when I run it can't load the class
So in the end, I'm a bit confused and I don't know if I explained clearly what I'm doing.
I think I would like to know in a simple way how people are doing it, rather than trying to correct my steps as I'm probably creating a mess myself.
Any help or suggestion welcome.
I'm using the last version of eclipse. I don't know which other tool's versions are relevant.
P.S. I also refresh,restart,clean rebuild the project often after touching things...but it doesn't get better
Maven is a build (management) tool. Simply spoken, its task is to create a JAR that can be used as a dependency/library by other projects or when running java -jar ....
Running a project's code isn't part of it (apart from unit and integration tests code and by using non-default plugins for special situations). Running code is part of Eclipse (or any other IDE) with its Run Configurations.

How to create a launch configuration "Java Application" that uses Maven Dependencies from scope=test for its classpath?

This question is a bit related to this one.
Problem Description
I want to create a new Launch Configuration of type "Java Application" (not JUnit!) that will start some kind of stand-alone test tool that we have.
This test tool is also needed during the Maven Build phase integration-test and is therefore included in the pom.xml. However, the dependency is in <scope>test</scope>.
The problem is that the Maven Dependencies which I add to the classpath of my launch configuration does not contain the dependency to this test tool library which makes it obviously impossible to e.g. find the MainClass that shall be run.
Question
Is it possible to create a launch configuration that uses the Maven Dependencies of the scope "test"?
Remarks
Note, that I do not want to create a "Maven Build" launch config as I want to run the test tool as a stand-alone Java application.
EDIT
"Solution"
I ended up creating a JUnit Test case that does nothing but calling the main method of the stand-alone application. It is very dirty but it gets the job done.

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 build a scala application that was created in eclipse scala plugin FROM THE CL

I have developed a scala application for the first time, but I have to deploy it with a "one-click" type script that can run and build the scala application from source WITHOUT ECLIPSE.
Since I'm completely new to scala I don't know how to tell it where all my source files are etc... to get it to build my app from the command line. I also have 2 3rd party .jar libraries that I need to tell the scala compiler to link to...
Any documentation on this? Or example command lines? My project hierarchy is:
src/packagename: contains all .scala
bin/packagename: contains all.class files
libs/ -> contains 2 .jar files I will need to import somehow
I'm working on debian linux
EDIT: I found this ability to export in eclipse so I created a .java file and called my main scala object from it. Then I exported as a runnable jar. However, when I go to run the new runnable jar "sudo java runnable.jar" it says "class not found exception: runnable.jar"
You should take a look at https://github.com/harrah/xsbt/wiki which is the common way to build a Scala project. Run through the tutorial in the wiki to learn how you should organise your directory structure, so that everything may run fine.
If you want to combine it with eclipse, checkout this plugin: https://github.com/typesafehub/sbteclipse

How to run scala code on Intellij Idea 11?

I am new to intellij idea and I decided to shift because I found the scala plugin on eclipse to be annoyingly buggy.
But, intellij idea, which I have heard to be very good, looks difficult to use.
I looked through the getting started with scala plugin page but couldn't find documentation specific to the latest version of lightweight ide of intellij idea 11 for scala.
I have created a project and made a hello world object in scala but I am not able to run it.
On trying to run it, it shows the scala interpreter (type in expressions to get them evaluated).
I don't exactly understand what changes to make in the project structure to be able to run scala code. As of now, i added the jsdk to point to my $JAVA_HOME and the scala library is also added. The scala plugin is also working in that syntax highlighting etc is fine.
The intellij idea user interface is also not very friendly and I don't get how to run the code. What modules am I supposed to add? Also, does compiled scala code run with jvm?
I'll be grateful to anyone who helps out.. I have been putting off writing scala code because I am not able to find THE IDE.
EDIT: Thanks for all the help! I am now able to run a basic scala program. I am trying to add external jars. What is the equivalent of doing Project right click-> Configure Build Path -> adding external jar files in eclipse? I tried adding "dependencies " under modules tab in project structure. It's adding the jar files but somehow the classes in the jar files are not still recognised.
With the Scala plugin installed either
A. Create a new project and select the Scala facet in the creation phase. It will create the Scala library lib and Scala compiler lib and setup the facet for you
B. If you already have a project. Go to Project Structure -> Modules and right click the module and go Add facet and add a Scala facet. Now you need to add scala-library.jar as a library of the module and go into the Scala facet and point it to a library containing scala-compiler.jar
Some more information
this is what your module should look like under project settings
select the Scala facet and this is what you should see (Library name for the compiler is unimportant as long as it says (version xxx) next to the library name
these are the jar files in my scala-compiler lib
and these are the jar files in my scala-library lib
With everything setup like that you should be able to right click -> new Scala class (Select object from the dropdown) in a source directory and add
def main(args:Array[String]) {
println("Hello world")
}
then right click on the class and select Run Classnam.main() and IntelliJ will setup a run configuration for you.
Edit: I can see someone posted somewhat the same I want to say here. I hope this can give a few more in-depths hints, so I will post it anyway :-)
If you would like to run Scala native in IntelliJ (I shifted to IntelliJ and still can't get my hands down) you need to make sure a few things are in order. Most of this happens under File -> Project Structure which requires you to create a project (when you get a hang of it, it is much more logical than Eclipse -- and Netbeans for that matter). I am also assuming you have selected a jdk, but this can also be done in the Project Structure.
Now, to Scala:
Make sure you have the Scala plugin. Visit the Settings (File -> Settings; can also be found at the start-up screen) and locate 'Plugins' (for me it's nr. 8 from the bottom). Under "Browse Repositories" you should be able to search for "scala", find and install a plugin.
1 A. If no plugins pops up you are probably using a version of intellij where scala is not (yet) supported. If that happens you can go to the homepage for the plugin and download the nightly build.
Import Scala as a library in File -> Project Structure -> Global Libraries. Click the plus sign, select Java (Scala is a Java-lib), browse to your scala-dir (which can be downloaded here: http://plugins.intellij.net/plugin/?id=1347) and select the lib directory. Press ok and you should see the library popping up on the list.
Make sure the facet (Scala framework) is in place. Goto File -> Project Structure -> Facets. Click on the plus-sign and find Scala. A sublink should appear (if not double-click the Scala link). Clicking the sublink make sure you have selected a compiler before continuing. This should be provided by the modules selected before. I normally use FSC (Fast Scala Compiler) but it's probably a good idea to test the basic settings before experimenting.
Make sure the Scala Module is in place. In Project Structure -> Modules click on the module you want to compile in scala and click the plus icon above and select Scala. Again be sure to select a compiler library.
If you want to, setup configurations for the compiler under the facet
Use sbt with sbt-idea plugin to generate idea project files for complex projects.
Or
For simple projects.
Add scala nature to the project, and you can right click and run any file extending App.
A good walk-through on how to set up a Scala project in Intellij is located here:
http://sonyarouje.com/2011/03/18/running-scala-in-intellij-idea-10/