Sbt test file cannot find value from src file - scala

My sbt project with scala-test is layout as standard:
sbt-example
build.sbt
src/main/scala/local/search/BinarySearch.scala
src/main/scala/local/util/Utility.scala
src/test/scala/local/search/SearchingSuite.scala
My build.sbt is very simple:
name := "helloworld"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.4"
)
My BinarySearch.scala looks like:
package local.search
object BinarySearch {
// def ...
}
My SearchingSuite.scala looks like:
package local.search
import org.scalatest.FunSuite
class SearchingSuite extends FunSuite{
// call functions from BinarySearch object
}
Run commands are:
sbt compile
sbt "test-only local.search.SearchingSuite"
Then sbt raises error:
not found: value BinarySearch
What's wrong with my code? And if I need to use some object from Utility.scala, how to import in SearchingSuite.scala?
Thanks, will vote up with any answers!

Let's say you are working on class A; and class A requires class B. As long as A and B belong to two different packages, you have to import class B.
In your case, SearchingSuite and BinarySearch belong to two different packages. In particular, the former belongs to the main source package, the latter belong to the test source package. Therefore, you must import BinarySearch in SearchingSuite. Otherwise, there will be an compilation error like what you have encountered.
To use some object from Utility, you
import local.util.Utility
if Utility is a class, or
import local.util.Utility._
if Utility is an object.
If you are using some IDE, like Intellij or Eclipse, right click on the error highlight, the IDE will pop up some suggestions.

Related

Unable to stub controller components

The method stubControllerComponents in package play.api.test appears to use same package and object name as a separate dependency which is causing a conflict when I attempt to use stubControllerComponents :
play.api.test.Helpers.stubControllerComponents is not found in below code:
import java.io.File
import play.api.test
import play.api.mvc._
import javax.inject._
import play.api.Environment
import play.api.mvc.{AbstractController, ControllerComponents}
class CountController #Inject() (cc: ControllerComponents,
env: Environment) extends AbstractController(cc) {
def getter() = Option(env.classLoader.getResourceAsStream("file.csv"))
}
play.api.Environment(play.api.test.Helpers.stubControllerComponents, Environment.simple())
This Helpers contains the method I require stubControllerComponents :
But this version of the class is being imported with import play.api.test :
Play link for stubbing : https://www.playframework.com/documentation/2.6.x/Highlights26#StubControllerComponents
build.sbt:
name := "ddd"
version := "1.0"
lazy val `ddd` = (project in file(".")).enablePlugins(PlayScala)
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
resolvers += "Akka Snapshot Repository" at "https://repo.akka.io/snapshots/"
scalaVersion := "2.12.2"
libraryDependencies ++= Seq( jdbc , ehcache , ws , guice , specs2 % Test)
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
Do I need to exclude portions of a dependency, in this case filters-helpers in order to make stubControllerComponents available ?
Update:
play.api.test.Helpers.stubControllerComponents not found:
Update2:
You seem to be using a scratch file. AFAICS, there is no way to also include dependencies from your Test scope into the classpath of your worksheet.
A workaround would be to (temporarily) add the play-test artefact to your libraryDependencies. Or just create a proper test file, which has access to the Test libraries normally.
play.api.test.Helpers.stubControllerComponents is provided by play-test
dependency
libraryDependencies += "com.typesafe.play" %% "play-test" % PlayVersion.current % "test",
which is indirectly imported by Play's sbt plugin specified at project/plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.1")
after explicitly enablePlugins(PlayScala) within project's build.sbt.
Note how play-test is out-of-the-box scoped to test configuration hence it is provided only on the test classpath. If you wish to reference stubControllerComponents from within IntelliJ Scala Worksheet, then make sure to create the worksheet inside test/ directory and not inside app/ directory. This will make Scala Worksheet use test classpath.

object mockito is not a member of package org

Relatively new to sbt and Mockito.
I want to use Mockito in tests, but I'm getting errors related to the Mockito imports when I compile the tests
Imports in test file:
import org.scalatest._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
sbt file:
name := "blah"
version := "0.1"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
libraryDependencies += "org.mockito" % "mockito-core" % "1.8.5" % "test"
I get these error messages when the tests (fail to) compile:
object mockito is not a member of package org [error] import org.mockito.Mockito._
and also:
Symbol 'type org.mockito.MockSettings' is missing from the classpath.
[error] This symbol is required by 'value org.scalatest.mockito.MockitoSugar.mockSettings'.
I've had a play around with changing some of the versions of scalatest and mockito in the sbt file, but not really if that's getting at the root of the problem or not.
Thanks for any help!
You're using a very old version of Mockito, which is older than the one Scalates relies on, you probably need some 2.x.x version.
On the other hand, I'd recomend you to go fo mockito-scala rather than mockito-core and skip the Scalatest provided classes altogether as they are quite basic.
I suspect you have a caching problem. This happens especially with Intellij.
Here 2 ideas:
Reload the sbt project. See https://stackoverflow.com/a/20466144/2750966
Close the project / delete .idea an open the project newly with Intellij.
Let me know if it is not related with Intellij

Plugins compile order in SBT

I'm trying to create an SBT plugin that provides common plugins and settings for my organization's projects (something like a Maven parent POM).
Since most of these settings should also be present in the commons project itself, I'm adding the sources as unmanagedSourceDirectories in plugins.sbt so the autoplugins I define for the proper project are also present on the meta-project (a neat trick I took from sbt-release plugin).
Things work just fine, except I still need to duplicate the addSbtPlugin entries when I want a plugin for booth my proper project and meta-project. If I don't do this, my proper-project won't compile since the plugins' classes are not loaded.
I tried moving all the shared plugin dependencies to a separate autoplugin assuming SBT would compile it and add the library dependencies to the meta-project so they would be there when my common plugin is compiled, but it doesn't work.
I would like to understand why this fails and if there is some way to thinker with the compilation order to somehow make it work. If not, I would gladly hear any alternatives you guys know to avoid having to maintain duplicate versions of all my shared plugins.
Here is a simplified version of my code:
project structure
/common
|-build.sbt
|-/project
| |-plugins.sbt
|-/src/main/scala/package
|-Dependencies.scala
|-MyCommonPlugin.scala
build.sbt
sbtPlugin := true
name := "common"
plugins.sbt
unmanagedSourceDirectories in Compile += baseDirectory.value.getParentFile / "src" / "main" / "scala"
Dependencies.scala
import sbt._
import sbt.Keys._
//Plugins I intend to share between build.sbt and plugins.sbt
object Dependencies extends AutoPlugin {
override def trigger = allRequirements
override lazy val projectSettings = super.projectSettings ++ Seq(
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5")
)
}
MyCommonPlugin.scala
import sbt._
import sbt.Keys._
//this import fails! object sbt is not a member of package com.typesafe
import com.typesafe.sbt.GitBranchPrompt.{ projectSettings => gitBranchPromptSettings }
object MyCommonPlugin extends AutoPlugin {
override def trigger = allRequirements
override lazy val projectSettings =
super.projectSettings ++
gitBranchPromptSettings ++
Seq(
// My common settings
)
}
To make this code work I would need to repeat the addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "0.8.5") on plugins.sbt.

how to use CSPARQL-ReadyToGoPack-0.9 in a SBT project

I want to use the CSPARQL-ReadyToGoPack-0.9 in an SBT project. I'm on Linux. How can I do that?
In your build script you probably need to specify an artifact explicitly for the dependency (the module id parameters are jus):
import sbt._
import Keys._
libraryDependencies +=
"foo" % "bar" % "0.1" artifacts Artifact("foo", url("file:/path/to/jar"))

Importing .jar files into Scala environment

Even after reading: Scala, problem with a jar file, I'm still a bit confused. I am trying to import some packages into my Scala file, and the interpreter is not recognizing them even after adding to classpath.
One example:
I have the import statement:
import org.json4s._
I downloaded the .jar from here: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4
and added to the interpreter classpath using:
scala> :cp /Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar
Scala acknowledges the classpath:
Your new classpath is: ".:/Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar:/Users/aspangher13/Downloads/jna-3.5.2.jar"
But still throws this error:
<console>:7: error: object scalatra is not a member of package org
import org.json4s._
Can anyone see what I'm doing wrong? Thanks!!
And as a followup, does anyone know where to find the package: JsonAST._?
Go the simple and create a little sbt project.
First step - create a project
For your purposes you don't need a complex build. So just create two files:
./build.sbt
name := "name your project"
version := "0.1"
scalaVersion := "2.10.2" // or whatever you prefer
./project/build.properties
sbt.version=0.12.4
The just go to the project root folder and call sbt
Second step - add dependencies
Open your ./build.sbt file and add:
libraryDependency ++= Seq(
"org.scalatra" %% "scalatra" % "2.2.1",
"org.scalatra" %% "scalatra-scalate" % "2.2.1",
"org.scalatra" %% "scalatra-specs2" % "2.2.1" % "test",
"org.json4s" %% "json4s-native % "3.2.4",
"net.java.dev.jna" & "jna" & "3.5.2"
)
Step three - run the console
Don't forget to reload sbt with reload task, and then call console or console-quick task. This should work.
But there are easier ways to do this:
1) Use gitter8 - Scalatra gitter8 project
2) Read little into about Scalatra sbt dependencies
Still not sure how :cp works but if you execute
scala -classpath "list of jars colon separated"
then inside the REPL do your imports it should work
import org.json4s._
import org.xyz
However, when you try to use the classes you are likely to be missing transitive dependencies required by json4s and so we come back to the sbt example # 4lex1v describes, which will handle this. Creating a little project and running sbt console will indeed greatly simplify this.
Seems like the -classpath and :cp are primarily meant to make your code available in the shell and then only if you understand all of the transitive dependencies, or have none.