Running Scala test from Intellij IDEA

2,142 views
Skip to first unread message

Eric Fowler

unread,
Dec 30, 2014, 1:29:19 AM12/30/14
to scala...@googlegroups.com
This should be really, really simple. 

I have an SBT project with main and test directories. In each there is code that builds and runs from the command line - some scala code and some unit tests. 

How do I run the unit tests from IDEA? I am going crazy with Run, Debug, etc. Some present me with options that I can't seem to use. The project builds OK, I just can't run the tests. 

Thanks

Eric

ogrecheng

unread,
Dec 30, 2014, 4:02:04 AM12/30/14
to scala...@googlegroups.com
right click your test class, and choose the `Run YourClassFile`.

Eric Fowler

unread,
Dec 30, 2014, 11:49:49 PM12/30/14
to scala...@googlegroups.com
I don't see that option. Basically I have the directory structure of an SBT project. I can choose the Run menu, then Edit Configurations, which brings up a dialog with an option to choose Scala Test , which in turn brings up a baffling dialog. 

Eric

Tim P

unread,
Dec 31, 2014, 12:08:46 PM12/31/14
to scala...@googlegroups.com
In the project view, right click on your test. There should be an option to run it. This auto-generates a run in the run configurations too.
Works for specs2 tests for sure.

Alternaively on Run/Debug Configurations click on the +  above the panel that says Defaults. Don't click on defaults - it allows you to specify defaults for runs - but you still have to create them. That's the + job.
Clicking on the + brings up a dropdown with many choices. One is Scala Test
In the TestClass click the ... and start typing in the name of your test. It should find it.
You also need to set the module (6th box on configuration tab). There's often only one to choose between (at least on my system).
Otherwise accept all defaults.
This assumes you're running intellij 13 or 14 - can't remember if 12 was the same.

ericacm

unread,
Dec 31, 2014, 12:12:55 PM12/31/14
to scala...@googlegroups.com
Make sure that 1) the src/test/scala directory is marked as Test Source (right click and choose Mark Directory as...) and 2) that your test class extends a ScalaTest trait such as FunSuite.

Eric Fowler

unread,
Dec 31, 2014, 10:50:22 PM12/31/14
to Tim P, scala...@googlegroups.com
Something must be wrong. There is no Run option when I right-click. 

If I choose Run/Debug configs and then '+', enter the name of my test class, it says "<test class> is not acceptable" with no further explanation. 

Eric 

--
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/4c9f2SiVmc0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Pederson

unread,
Dec 31, 2014, 11:00:54 PM12/31/14
to Eric Fowler, Tim P, scala-user
Which traits/classes is your test class extending?   What does it list when you right-click on src/test/scala and choose Mark Directory As?

Thanks,


-- Eric

Eric Fowler

unread,
Dec 31, 2014, 11:11:28 PM12/31/14
to Eric Pederson, Tim P, scala-user
Eh, Mark Directory As ... seems to have been a large part of the answer. Now I can build and run, and am seeing a run time error, which I will investigate. 

Thanks for your assistance. 

Eric

Eric Fowler

unread,
Jan 1, 2015, 11:53:52 PM1/1/15
to Eric Pederson, Tim P, scala-user
I have somehow managed to break this again. 

What should I mark as 'test sources root'? /src/test/ or /src/test/scala? Likewise, sources root - /src/main or /src/main/scala? 

Each brings its own errors .. if I choose not to mark any directories, my build works but I get 'test class not found' when I run. Also the chromacoding is hosed. 
 
If I mark /src/main/scala + /src/test/scala, or  /src/main and /src/test, I get this monster: 

Error:scalac: 
     while compiling: C:\Users\Eric\Shuttle\src\main\scala\Car.scala
        during phase: typer
     library version: version 2.10.4
    compiler version: version 2.10.4
  reconstructed args: -nobootcp -classpath C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\rt.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Users\Eric\Shuttle\out\production\routing;C:\Users\Eric\.ivy2\cache\org.scala-lang\scala-library\jars\scala-library-2.11.4.jar;C:\Users\Eric\.ivy2\cache\org.scala-lang\scala-reflect\jars\scala-reflect-2.11.4.jar;C:\Users\Eric\.ivy2\cache\org.scalatest\scalatest_2.11\jars\scalatest_2.11-2.2.1.jar -javabootclasspath ;
  last tree to typer: Literal(Constant(()))
              symbol: null
   symbol definition: null
                 tpe: Unit
       symbol owners: 
      context owners: object Car -> package <empty>
== Enclosing template or block ==
ModuleDef( // object Car
  <module>
  "Car"
  Template(
    "java.lang.Object" // parents
    ValDef(
      private
      "_"
      <tpt>
      <empty>
    )
    // 2 statements
    DefDef( // def <init>: <?> in object Car
      <method>
      "<init>"
      []
      List(Nil)
      <tpt>
      Block(
        Apply(
          super."<init>"
          Nil
        )
        ()
      )
    )
    <empty>
  )
)
== Expanded type of tree ==
TypeRef(TypeSymbol(final abstract class Unit extends AnyVal))
uncaught exception during compilation: scala.reflect.internal.MissingRequirementError


Suddenly nothing is working ... weird. 

Eric

On Wed, Dec 31, 2014 at 7:59 PM, Eric Pederson <eri...@gmail.com> wrote:

Tim Pigden

unread,
Jan 2, 2015, 5:13:27 AM1/2/15
to Eric Fowler, Eric Pederson, scala-user
Eric
I don't do any of this stuff.
Try the following:
- exit intellij
- delete or rename your intellij .idea folders in the project.
- go to "open" the project 
- point it at your sbt file (which should have a little sbt icon) or at your project root - which will likewise have the icon.
- you get a dialog box about auto-imports. check all boxes
This way you've done it according to intellij expectations.
 
--
Tim Pigden
Optrak Distribution Software Limited
+44 (0)1992 517100
http://www.linkedin.com/in/timpigden
http://optrak.com
Optrak Distribution Software Ltd is a limited company registered in England and Wales.
Company Registration No. 2327613 Registered Offices: Suite 6,The Maltings, Hoe Lane, Ware, SG12 9LR England 
This email and any attachments to it may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of Optrak Distribution Software Ltd. If you are not the intended recipient of this email, you must neither take any action based upon its contents, nor copy or show it to anyone. Please contact the sender if you believe you have received this email in error.

Eric Pederson

unread,
Jan 2, 2015, 9:21:21 AM1/2/15
to Tim Pigden, Eric Fowler, scala-user
I agree with Tim.  That is the best approach for now.  Reiterating Tim's steps, adding a couple:
  1. exit intellij
  2. delete or rename your intellij .idea folders in the project.  also delete any *.iml files and the target directory.
  3. restart intellij
  4. choose "open" or "open project" (depending on if you have intellij 14 or 13) 
  5. in the file browser, choose your sbt file (which should have a little sbt icon) or your project root - which will likewise have the icon.
  1. you get a dialog box about auto-imports. check all boxes
    What will happen after that is done is that Intellij will have configured everything automatically.  For example it will mark the src/main/scala and src/test/scala as Sources Root and Test Sources Root, respectively.


    -- Eric

    Eric Fowler

    unread,
    Jan 3, 2015, 12:44:41 AM1/3/15
    to scala...@googlegroups.com, tim.p...@optrak.com, eric....@gmail.com
    For the record: this worked, and I thank each of you.

    Eric
    Reply all
    Reply to author
    Forward
    0 new messages