here is my test case , while i right click the file eclipse doest not show any run as junit test option. I try to manual create run configuration but does not take any sense.
scala version:2.8.1 scalatest:1.3 eclipse:3.6.2
package org.jilen.cache.segment
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
#RunWith(classOf[JUnitRunner])
class RandomSegmentSpec extends FlatSpec with ShouldMatchers {
val option = SegmentOptions()
"A Simple Segment" should "contains (douglas,lea) after put into" in {
val segment = RandomSegment.newSegment(option)
segment.put("douglas", "lea")
segment("douglas") should be("lea")
}
it should "return null after (douglas,lea) is remove" in {
val segment = RandomSegment.newSegment(option)
segment.put("douglas", "lea")
segment -= ("douglas")
segment("douglas") should equal(null)
}
it should "contains nothing after clear" in {
val segment = RandomSegment.newSegment(option)
segment.put("jilen", "zhang")
segment.put(10, "ten")
segment += ("douglas" -> "lea")
segment += ("20" -> 20)
segment.clear()
segment.isEmpty should be(true)
}
}
I've encountered this seemingly randomly, and I think I've finally figured out why.
Unfortunately the plugin doesn't yet change package declarations when you move files, nor the class names when you rename files. (Given you can put multiple classes in one file, the latter will likely never be done.) If you are used to the renamings being done automagically in Eclipse, like I am, you're bound to get caught on this.
So... check carefully the following:
the package declaration in your Scala file matches the Eclipse package name
the name of the test class in the Scala file matches the name of the Scala file
I just ran into this, fixed both, and now my test runs!
This is a known problem with the Eclipse IDE for Scala. I'm currently working on the plugin for this. Watch this space.
I found Scalatest to be very bad at integrating with Eclipse (running the tests from eclipse showed that it ran them - but they would not pass or fail, but simply show up as passive blank boxes).
For some reason I could NOT get it to work after 3 hours of trying things!
Finally I tried specs2 - and it worked (Scala 2.9, Junit4 and Eclipse 3.6)!
They have a great doc here:
http://etorreborre.github.com/specs2/guide/org.specs2.guide.Runners.html#Runners+guide
Since I don't care which testing framework to use, I will try Specs2 purely from the convenience point of view.
Related
I'm new to writing junit tests in Scala and I'm using Mockito to mock objects. I'm also using scalatest_2.12-3.0.4. The ScalaTest documentation (like here) shows the syntax to create the mock using MockitoSugar, i.e.
val mockCollaborator = mock[Collaborator]
Eclipse shows org.scalatest.mock.MockitoSugar crossed out in the import statement, indicating it is deprecated. The only alternative I've found is, don't use MockitoSugar and instead do:
val mockCollaborator = mock(classOf[Collaborator])
This seems to work fine too, so I'm curious what ScalaTest is recommending me to use.
Or for that matter, why else would I use MockitoSugar? Does it have any other features? I've been unable to find any documentation about it, except that everybody seems to use the shorthand mock[] notation.
Base on the issue and some comments, the answer should be changed:
the MockitoSugar has been moved to a separate project: https://github.com/scalatest/scalatestplus-mockito
need to add a new dependency to use it
https://mvnrepository.com/artifact/org.scalatestplus/mockito-3-4_2.13/3.3.0.0-SNAP3
testCompile group: 'org.scalatestplus', name: 'mockito-3-4_2.13', version: '3.3.0.0-SNAP3'
origin issue: https://github.com/scalatest/scalatest/issues/1789
As reported in the source you should use org.scalatest.mockito.MockitoSugar instead of org.scalatest.mock.MockitoSugar
I am working on a big sbt project and there is some functionality that I want to benchmark. I decided that I will be using jmh, thus I enabled the sbt-jmh plugin.
I wrote an initial test benchmark that looks like this:
import org.openjdk.jmh.annotations.Benchmark
class TestBenchmark {
#Benchmark
def functionToBenchMark = {
5 + 5
}
}
However, when I try to run it with jmh:run -i 20 -wi 10 -f1 -t1 .*TestBenchmark.* I get java.lang.InternalError: Malformed class name. I have freshly rebuilt the project and everything compiles and runs just fine.
The first printed message says
Processing 6718 classes from /path-to-repo/target/scala-2.11/classes
with "reflection" generator
I find it weird that the plugin tries to reflect the whole project (I guess including classes within the standard library). Before rebuilding I was getting NoClassDefFoundError, although the project was otherwise working well.
Since there are plenty of classes within the project and I cannot make sure that every little bit conforms to jmh's requirements, I was wondering if there's a way to overcome this issue and focus and reflect only the relevant classes that are annotated with #Benchmark?
My sbt version is 0.13.6 and the sbt-jmh version is 0.2.25.
So this is an issue with Scala and Class.getSimpleClassName.
Its not abonormal in Scala to have types like this:
object Outer {
sealed trait Inner
object Inner {
case object Inner1 extends
case object Inner2 extends Inner
}
}
With the above calling Outer.Inner.Inner1.getClass().getSimpleName() will throw the exception your seeing.
I don't think it uses the full project, but only for things that are directly referred to in the State or Benchmark.
Once I had my bench file written that way it worked.
New to Intellij IDEA/Scala so I'm wondering is there shortcut to auto import Scala packages.
Example:
package test
object TestClass extends App{
var i = pow(22,22)
println("Hello World" + i );
}
It wont compile until import statment is added
import scala.math._
Coming from Eclipse/Java I expected CRTL + Shift O (or auto import) would offered me math package, must I type import myself ?
Sometimes yes, sometimes no. It depends on what you're searching for.
If you write math IntelliJ doesn't know what that is. If you write Math., that's already in scope and it will offer a menu of methods on the Math object.
If you write Date, alt-enter should bring up a menu of import options. Choose one and the import statement will be inserted into your code.
No, not necessarily.
In your settings in IntelliJ you can set up auto import by following these instructions. Alternatively, when you try and use a package that you do not have imported, it will tell you that it does not recognize what you are doing and show a red error. You can then autofill from the error (typically hit alt+enter) and it should solve the issue.
complete stranger/newbie question, sorry to bother you:
I want to run a scala project in IDEA 13 (on Mac OSX), there is no run/debug configuration. I tried some options, however I don't know what to choose (and what should happen, then).
I tried:
Scala Console
Scala Script
Scala Test
Specs2
Choosing Scala Console: something happens, but I get no message box from my showMessage(message = "Hello World") messagebox. (It did work in Eclipse, so far).
Scala Script: I suppose this is meant for single lines of code, not an app, is that?
Specs2: Error in options-dialog: Specs2 is not specified. What is spec2? What should I enter and where? Error while debugging: no main class detected. OK, there is none, I can see that.
Any other option I should use, or normally is used?
Would any help appreciate very much!
This is my (very simple) scala app. Somehow, I'd expect to get an message box while running from IDEA.
import swing._
import swing.Dialog._
import scala.swing.event.ButtonClicked
object HW {
def main(args: Array[String]): Unit = {}
showMessage(message = "Hello World")
}
by the way: the closest tread I've found was this, but it didn't really help, looked too different:
How to run scala code on Intellij Idea 11?
Just use "Application". You have to select a main class and a module.
In terms of your example, I recommend not mixing a def main with a "free" standing constructor body. Also, Swing code should be called only from the event dispatch thread (Swing.onEDT). The easiest for desktop applications is to extend trait SwingApplication which handles that for you:
import swing._
import swing.Dialog._
object HW extends SwingApplication {
def startup(args: Array[String]): Unit =
showMessage(message = "Hello World")
}
I have a multi-project Scala workspace in eclipse. I think I'm getting hosed by my lack of understanding of the way Scala imports packages, but after spending more time than I care to admit looking for a solution, I can't figure this one out. I have recreated the problem in a simple 2 project setup.
Project 1: com.foo.mathematics contains a simple Vector class
Contains one file:
package com.foo.mathematics
class Vector2D(x : Double, y : Double) {
def length = math.sqrt(x*x + y*y)
}
Project 2: com.foo.analysis
package com.foo.analysis
import com.foo.mathematics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}
Eclipse shows an error in the import line, The error message that I get is: Object mathematics is not a member of the package com.foo.
In the outline view, my import statement says this:
com.foo.analysis.<error: <none>>.Vector2D
I have tried changing the import to:
import mathematics.Vector2D
import _root_.com.foo.mathematics.Vector2D
neither one works...
What am I missing?
Both import com.foo.mathmatics.Vector2D and import _root_.com.foo.mathmatics.Vector2D should be fine. Most likely you either haven't added the first project to the build path of the second (see Build Path > Configure Build Path in the context menu), or need to clean the second project (Project > Build Clean) after making changes in the first project.
(Also, mathmatics looks like a typo for mathematics, so double check that you really have the same name in both places.)
Relative package imports don't come into it, they just mean you could write it this way:
package com.foo
package analysis
import mathmatics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}