Why am I getting object scalatest is not a member of package org - scala

I am trying to follow the Scala tutorial and am getting the error object scalatest is not a member of package org. All of the other instances of this error I can find here and on the web have the problem that the test file isn't under src/test, but that's not the case for me. I repeat, for the sake of those who keep marking this as a duplicate, the test file is under src/test, so that is not the problem. My build.sbt file says:
name := "TestExercise"
version := "0.1"
scalaVersion := "2.12.6"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % "test"
src/main/scala/CubeCalculator says:
object CubeCalculator extends App {
def cube(x: Int) = {
x * x * x
}
}
and src/test/scala/CubeCalculatorTest says:
import org.scalatest.FunSuite
class CubeCalculatorTest extends FunSuite {
test("CubeCalculator.cube") {
assert(CubeCalculator.cube(3) === 27)
}
}
(Cut-and-pasted straight from the tutorial.)
So what am I doing wrong? Why can't my project access scalatest?

#terminally-chill gave the answer. I am using Intellij, and File | Invalidate caches / restart, followed by a rebuild, resolved the issue.

Related

Issue in testing using scalatest

I just set up a new scala project with sbt in IntelliJ, and wrote the following basic class:
Person.scala:
package learning.functional
case class Person(
name: String
)
Main.scala:
package learning.functional
import learning.functional.person
object Main{
val p = Person("John")
}
PersonTest.scala:
import learning.functional.Person
import org.scalatest.FunSuite
class PersonTest extends FunSuite {
test("test person") {
val p = Person("John")
assert(p.name == "John")
}
}
When I try to run sbt test, it throws the following error:
## Exception when compiling 1 sources to /Users/johndooley/Desktop/Scala/scala-learning/target/scala-2.13/test-classes
[error] java.lang.AssertionError: assertion failed:
[error] unexpected value engine in trait FunSuite final <expandedname> private[this]
[error] while compiling: /Users/johndooley/Desktop/Scala/scala-learning/src/test/scala/PersonTest.scala
What could be the reason for this? My build.sbt file:
ThisBuild / scalaVersion := "2.13.10"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "1.9.1"
lazy val root = (project in file("."))
.settings(
name := "scala-learning"
)
Project structure:
I have tried invalidating cache and restarting, doing clean, update, compile, but nothing works.
I solved this by modifying my dependency to the following:
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.14"
and completely reloading the sbt shell

How do I run sbt test with scalatest? I had an error: object scalatest is not a member of package org

I want to test my code in the following project using scalatest.
MyProject/
+ src
- main/scala/mypackage1/Calc.scala
- test/scala/mypackage1/CalcTest.scala
- built.sbt
built.sbt
libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.2"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.2" % "test
Calc.scala
package mypackage1
object Calc extends App {
def add(x: Int, y: Int) = x + y
}
CalcTest.scala
package mypackage1
class CalcTest extends org.scalatest.funsuite.AnyFunSuite {
test("Calc.add") {
assert(Calc.add(1, 2) == 3)
}
}
I run sbt test in test/scala/mypackage1 but I got a compile error.
What should I do?
[error] /workspaces/MyProject/src/test/scala/myproject1/CalcTest.scala:3:28: object scalatest is not a member of package org
[error] class CalcTest extends org.scalatest.funsuite.AnyFunSuite {
[error] ^
Sorry but I resolved this myself.
I run stb test in MyProject directory (root directory of the project, not in test/scala/mypackage1) and it worked properly.
Thank you for your help.

Unable to make reference to object in build.sbt, "error: not found: value hooks"

I'm trying to add gulp to my Play application, I have created a PlayRunHook object that should allow me to trigger the gulp command, but when I do sbt run I'm getting an error saying it could not find the object. Here is my hook:
package hooks
object Gulp extends CommandHook {
override def beforeStarted(): Unit = {
exec("gulp")
}
}
And then in build.sbt:
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
scalacOptions ++= Seq("-deprecation")
libraryDependencies ++= Seq(
"junit" % "junit" % "4.10" % "test",
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.14",
"com.typesafe.play" %% "play" % "2.5.0",
"com.typesafe.play" %% "play-netty-server" % "2.5.0"
)
PlayKeys.playRunHooks += hooks.Gulp()
But I get:
build.sbt:18: error: not found: value hooks
PlayKeys.playRunHooks += hooks.Gulp()
You have to create Gulp object in project/ directory like below:
import play.sbt.PlayRunHook
import sbt._
object Gulp {
def apply(base: File): PlayRunHook = {
object GulpProcess extends PlayRunHook {
override def beforeStarted(): Unit = {
Process("gulp", base).run
}
}
GulpProcess
}
}
Then in your build.sbt:
PlayKeys.playRunHooks += Gulp(baseDirectory.value)
For more details check this guide.
Edit: I actually went with the accepted answer in the end, this alternative answer will trigger your gulp bundle command every time a request is made to your app. By using the PlayRunHook approach, you get different lifecycle hooks to hook into, and in the end I hooked into beforeStarted (as done in the accepted answer) to trigger gulp watch when starting the app.
Down the line I may alter this to do gulp watch in development and gulp build when deploying, though I am not at that stage yet.
The accepted answer explains why build.sbt cannot find the value hooks (the code needs to be in the directory project/), however to solve my issue of wanting to run gulp bundle during compile, I went with this solution:
import sbt._
// ...
lazy val gulp = taskKey[Unit]("Build frontend assets")
gulp := {
println("gulp bundle" !!)
}
compile in Compile <<= (compile in Compile).dependsOn(gulp)

Scala ambiguous imports

Scala beginner here and I was trying out the example here:
https://raw.githubusercontent.com/sryza/aas/master/ch02-intro/src/main/scala/com/cloudera/datascience/intro/RunIntro.scala
val nasRDD = parsed.map(md => {
md.scores.map(d => NAStatCounter(d))
})
The above gives me error:
<console>:51: error: reference to NAStatCounter is ambiguous;
it is imported twice in the same scope by
import $VAL180.NAStatCounter
and import INSTANCE.NAStatCounter
md.scores.map(d => NAStatCounter(d))
^
Can anyone please explain why this double import is happening. How can I avert this?
I could not reproduce your problem. I put RunIntro.scala into a small sbt project and compiled it successfully with the build.sbt file (with blank lines removed)
% cat build.sbt
name := "RunIntro"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++= List("org.apache.spark" % "spark-core_2.11" % "1.6.1")
The imports are only part of the source of your problem. How are you compiling this source?

How to add 'testListener' to custom test reporter in SBT

I'm having difficulty implementing a custom test reporter in Scala using SBT.
I have a basic SBT project set up with this build.sbt file:
name := "Test Framework"
version := "0.1"
scalaVersion := "2.11.7"
scalacOptions += "-deprecation"
scalacOptions += "-feature"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.12.4" % "test"
testFrameworks += new TestFramework("custom.framework.MyTest")
testListeners += new MyTest()
My MyTest.scala class is located in the projectsfolder under projects/custom/framework/MyTest.scala and looks like this:
import sbt.TestsListener._
import sbt.TestReportListener._
class MyTest extends TestReportListener {
def doInit(): Unit = {
println("Starting Test")
}
def testEvent(event: TestEvent): Unit = {
println(event.result.get)
}
def startGroup(name: String): Unit = {
println("Group Started")
}
}
The documentation here is sparse, and i'm obviously missing something. It states that i need to
Specify the test reporters you want to use by overriding the testListeners setting in your project definition. Where customTestListener is of type sbt.TestReportListener.
This should be possible by doing testListeners += customTestListener.
Since my class MyTest extends TestReportListener i thought i could just do testListeners += custom.framework.MyTest or testListeners += new custom.framework.MyTest, but this clearly is not the case.
I'm running sbt test to execute the tests, and the output is
error: not found: value custom
testListeners += new custom.framework.MyTest
I'm not sure how this is supposed to work.
Does anyone know how this is done correctly?
I did not figure out why stating the package did not work, but moving the MyTest.scala file out to the project directory, and removing the custom.frameworkpackage made it work.