Why does sbt say "bad symbolic reference..." for test with ScalaTest? - scala

I started using ScalaTest with sbt. The build.sbt is as follows:
name := "MySpecSample"
version := "1.0"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.0" % "test"
scalaVersion := "2.10.3"
Initial test code is here. This test code runs alone without main component code.
import collection.mutable.Stack
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
it should "throw NoSuchElementException if an empty stack is popped" in {
val emptyStack = new Stack[Int]
a [NoSuchElementException] should be thrownBy {
emptyStack.pop()
}
}
}
I wrote it according to Official quick start.
But it doesn't work properly. The error message is as follows:
> test
[info] Compiling 1 Scala source to /Users/kaisasak/untitled/target/scala-2.10/test-classes...
[error] bad symbolic reference. A signature in package.class refers to type compileTimeOnly
[error] in package scala.annotation which is not available.
[error] It may be completely missing from the current classpath, or the version on
[error] the classpath might be incompatible with the version used when compiling package.class.
[error] /Users/kaisasak/untitled/src/test/scala/ExampleSpec.scala:6: Reference to class FlatSpec in package scalatest should not have survived past type checking,
[error] it should have been processed and eliminated during expansion of an enclosing macro.
[error] class ExampleSpec extends FlatSpec with Matchers {
[error] ^
[error] two errors found
[error] (test:compile) Compilation failed
[error] Total time: 3 s, completed Jun 29, 2014 11:20:41 AM
I can find same problems on Google but no good answer was available.
My environment is here.
MacOSX 10.9.3
Scala 2.10.3
sbt 0.13.2
I tried with sbt 0.13.1 after downgrading, but the result is the same to the one of 0.13.2. What should I do to use ScalaTest with sbt?

In your build.sbt you set the version of Scala to 2.10.3 with the following:
scalaVersion := "2.10.3"
However the ScalaTest dependency uses Scala 2.11 explicitly (note the _2.11 part):
"org.scalatest" % "scalatest_2.11" % "2.2.0" % "test"
You have to use the same major scala version for both.
With sbt you can simply ensure this using %% instead of % as follows:
"org.scalatest" %% "scalatest" % "2.2.0" % "test"

Related

Play Framework: [Error] object inject is not a member of package javax

My play framework application is all of a sudden throwing the following errors during compilation:
[error] /Users/ihorton/Client Work/DPaaS/coursera-scala/cookbook/app/controllers/HomeController.scala:5:14: object inject is not a member of package javax
[error] import javax.inject._
[error] ^
[error] /Users/ihorton/Client Work/DPaaS/coursera-scala/cookbook/app/controllers/HomeController.scala:11:2: trait Singleton is abstract; cannot be instantiated
[error] #Singleton
[error] ^
[error] /Users/ihorton/Client Work/DPaaS/coursera-scala/cookbook/app/controllers/HomeController.scala:12:23: not found: type Inject
[error] class HomeController #Inject()(controllerComponents: ControllerComponents) extends AbstractController(controllerComponents) {
[error] ^
^
Here is my use of javax.inject, #Inject and #Singleton in code:
import javax.inject._
#Singleton
class RecipeController #Inject()(controllerComponents: ControllerComponents) extends AbstractController(controllerComponents) {
...
I've read through what looks to be the same issue here, but trying sbt reload prior to compile doesn't work for me.
What's puzzling, unless I'm misreading the Play Framework 2.8.x documentation, is that it seems like javax.inject should be packaged with Play out of the box. So I'm not sure why this object would be missing.
I'm using the following versions:
Play: 2.8.8
Scala: 2.13.6
JDK: 17.0.1
Here's my build.sbt file to which I've added javax.inject which doesn't seem to resolve the issue.
name := """cookbook"""
organization := "com.ian-horton"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.6"
libraryDependencies ++= Seq(
guice,
"commons-io" % "commons-io" % "2.5",
"javax.inject" % "javax.inject" % "1",
"net.sourceforge.tess4j" % "tess4j" % "5.0.0",
"org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test,
"org.specs2" %% "specs2-core" % "4.13.0" % Test)
Happy to provide additional detail if need be.
Thanks in advance!
Update
Finding this other Stack Overflow Question I followed the steps manually added the Maven dependency for Javax.Inject to my project in Intellij which fixed the problem.
I'm keeping this question open though in the hopes that someone can perhaps shed some light on why this dependency isn't pulled in with Play.

Compile error scala project

I'm taking the coursera scala course. I downloaded the sample project, but I can not compile it. Giving me error when I run the console command.
build.sbt:
name := course.value + "-" + assignment.value
scalaVersion := "2.12.4"
scalacOptions ++= Seq("-deprecation")
// grading libraries
libraryDependencies += "junit" % "junit" % "4.10" % Test
// for funsets
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4"
resolvers += "Artima Maven Repository" at "http://repo.artima.com/releases"
// include the common dir
commonSourcePackages += "common"
courseId := "bRPXgjY9EeW6RApRXdjJPw"
Error:
> console
[info] Updating root
[info] Resolved root dependencies
[trace] Stack trace suppressed: run last *:coursierResolution for the full output.
[error] (*:coursierResolution) coursier.ResolutionException: Encountered 1 error(s) in dependency resolution:
[error] org.scalatest:scalatest_2.12:2.2.4:
[error] not found:
[error] /Users/joaonobre/.ivy2/local/org.scalatest/scalatest_2.12/2.2.4/ivys/ivy.xml
[error] /Users/joaonobre/.sbt/preloaded/org.scalatest/scalatest_2.12/2.2.4/ivys/ivy.xml
[error] /Users/joaonobre/.sbt/preloaded/org/scalatest/scalatest_2.12/2.2.4/scalatest_2.12-2.2.4.pom
[error] https://repo1.maven.org/maven2/org/scalatest/scalatest_2.12/2.2.4/scalatest_2.12-2.2.4.pom
[error] http://repo.artima.com/releases/org/scalatest/scalatest_2.12/2.2.4/scalatest_2.12-2.2.4.pom
[error] Total time: 1 s, completed Dec 22, 2017 4:16:17 PM
scala -version
Scala code runner version 2.12.4 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc.
Any idea how to fix it?
Thanks.
As suggested by #laughedelic, Scala 2.12 does not have scalatest-2.2.4. As a result, sbt cannot find the related scalatest pom file. You can pick any of the versions mentioned here.
For instance, try changing scalaTestDependency version to 3.0.5 in the CommonBuild.scala file
lazy val scalaTestDependency = "org.scalatest" %% "scalatest" % "3.0.5"
Here is the course link (Practice Programming Assignment of Week1)

sbt failure for java.sql.{Connector,DriverManager,ResultSet}

I am running into an issue when I run "sbt package" on a scala program. Here are the imports that I have in my scala program
import java.sql.{Connecion,DriverManager,ResultSet}
when I run sbt package, I get the below errors.
[error] /home/SriniNN/scala_cd/ReadDJDBC/src/main/scala/ReadDJDBC.scala:8: object Connecion is not a member of package java.sql
[error] import java.sql.{Connecion,DriverManager,ResultSet}
[error] ^
[error] /home/SriniNN/scala_cd/ReadDJDBC/src/main/scala/ReadDJDBC.scala:26: value getConnecion is not a member of object java.sql.DriverManager
[error] val jdbcRDD = new JdbcRDD(sc, () => DriverManager.getConnecion(url,username,password),
This is what I have in my build.sbt
name:= "ReadJDBC"
version:= "1.0"
scalaVersion:= "2.11.8"
libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "1.6.2"
Can you please help me resolve this issue?
You are just mis-spelling Connection. Note the t between the c and i. If you fix that typo then it should work

MissingResourceException: Can't find bundle for base name com/sun/rowset/RowSetResourceBundle (SBT project)

Running sbt test via console (v0.13.1 on Windows) throws a MissingResourceException: : Can't find bundle for base name com/sun/rowset/RowSetResourceBundle, locale en_CA when I try to create an instance of com.sun.rowset.CachedRowSetImpl object. The same code runs great if I use IntelliJ to run a Specs test; it only fails when trying to run via SBT console.
Here is the specs2 test I'm trying to run:
import org.specs2.mutable.SpecificationWithJUnit
import javax.sql.rowset.CachedRowSet;
import com.sun.rowset.CachedRowSetImpl
class DatabaseTest extends SpecificationWithJUnit {
"CachedRowSet Test" should {
"Create a new CachedRowSetImpl instance" in {
val rowSet: CachedRowSet = new CachedRowSetImpl()
rowSet must_!= null
}
}
}
And the resulting exception:
[error] MissingResourceException: : Can't find bundle for base name com/sun/rowset/RowSetResourceBundle, locale en_CA (null:-1)
[error] com.sun.rowset.JdbcRowSetResourceBundle.<init>(Unknown Source)
[error] com.sun.rowset.JdbcRowSetResourceBundle.getJdbcRowSetResourceBundle(Unknown Source)
[error] com.sun.rowset.CachedRowSetImpl.<init>(Unknown Source)
[error] test.DatabaseTest$$anonfun$1$$anonfun$apply$1.apply(DatabaseTest.scala:10)
[error] test.DatabaseTest$$anonfun$1$$anonfun$apply$1.apply(DatabaseTest.scala:9)
Update: build.sbt contents:
scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature")
scalaVersion := "2.10.3"
scalacOptions ++= Seq("-Yrangepos")
javacOptions ++= Seq("-source", "1.6", "-target", "1.6", "-Xlint:deprecation", "-Xlint:unchecked")
libraryDependencies in ThisBuild ++= Seq(
"postgresql" % "postgresql" % "9.1-901.jdbc4" withSources(),
"org.specs2" %% "specs2" % "2.3.7" withSources()
)
lazy val root = project.in(file("."))
I had this issue when running ScalaTest using SBT. In IntelliJ the tests ran clean but in SBT the late bound implementation for CachedRowSet did not get resolved and the following exception was thrown,
MissingResourceException: : Can't find bundle for base name
com/sun/rowset/RowSetResourceBundle, locale en_GB
The problem appears to be that the SBT thread does not resolve run time class loads back to the JVM (the missing resources are provided by rt.jar and resources.jar).
This can be fixed by forcing the SBT thread to fork new threads for each test by putting the following instruction into the main body of the SBT build file,
fork in Test := true
CachedRowSetImpl is an internal class in some version of the JDK runtime but is not officially part of the JDK. You can tell it is internal because its package starts with "com.sun", not "java". It's likely that your Windows machine has a completely different JDK that lacks this internal class.
I highly recommend avoiding the use of "com.sun" classes and find the idiomatic way to get the job done with the existing JDK.
Provided you use Java 7...
As of Java 7 there're few classes and methods that shield you from errors like yours. Develop against interfaces (with some help from providers) and life becomes easier.
import org.specs2.mutable.SpecificationWithJUnit
import javax.sql.rowset._
class DatabaseTest extends SpecificationWithJUnit {
"CachedRowSet Test" should {
"Create a new CachedRowSetImpl instance" in {
val rsf = RowSetProvider.newFactory
val rowSet: CachedRowSet = rsf.createCachedRowSet
rowSet must_!= null
}
}
}
With the Specs2 test, test worked fine for me.
> test
[info] DatabaseTest
[info]
[info] CachedRowSet Test should
[info] + Create a new CachedRowSetImpl instance
[info]
[info] Total for specification DatabaseTest
[info] Finished in 41 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
BTW: The missing resources are in lib/resources.jar in JRE, but couldn't figure out why they're reported as missing (even when they ended up in lib directory of a project).
Having the same issue. Not in Scala, but Java.
com.sun.rowset.JdbcRowSetResourceBundle loads resource with context classloader.
In my case context classloader is a URLClassLoader.
And it looks like a really weird limitation of URLClassLoader - when looking for resources it does not consult parent classloader, it checks only its own URLs list.
Hope that helps.

scala Either and akka.dispatch Future missing while compiling

I am trying to create a REST service using Spray Servlet, but does not compile.
Below you can find my build.sbt:
name := "someservice"
version := "0.0.1"
scalaVersion := "2.10.0"
resolvers += "spray repo" at "http://repo.spray.io"
libraryDependencies += "io.spray" % "spray-servlet" % "1.0-M3"
libraryDependencies ++= Seq(
"io.spray" % "spray-servlet" % "1.0-M3",
"io.spray" % "spray-util" % "1.0-M3",
"io.spray" % "spray-http" % "1.1-M7",
"com.typesafe.akka" %% "akka-actor" % "2.1.0",
"org.specs2" %% "specs2" % "1.13" % "test",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" artifacts Artifact("javax.servlet", "jar", "jar")
)
There error I get com 'sbt update compile' is:
[error] bad symbolic reference. A signature in package.class refers to term Either
[error] in package scala which is not available.
[error] It may be completely missing from the current classpath, or the version on
[error] the classpath might be incompatible with the version used when compiling
[error] package.class.
[error] bad symbolic reference. A signature in package.class refers to type Future
[error] in package akka.dispatch which is not available.
[error] It may be completely missing from the current classpath, or the version on
[error] the classpath might be incompatible with the version used when
[error] compiling package.class.
[error] two errors found
[error]
(compile:compile) Compilation failed
Do I have to add the scala library to the sbt build file as well or am I missing something else?
tld;dr: try replacing 1.0-M3 with 1.1-M7
It looks like the Spray artifacts you're using were compiled against Scala 2.9.2, which is not binary compatible with Scala 2.10. Usually, Scala artifacts contain the Scala binary version in the artifact name, precisely because major Scala versions are not binary compatible.
Later spray-util milestones were compiled against Scala 2.10.0-RC5 (based on the pom), which technically should work.
Solved by using all the same spray versions (1.1-M7) and upgrading to Scala 2.10.1