Unable to stub controller components - scala

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.

Related

Add plugin's plugin dependency as plugin dependency of user

I am creating an SBT plugin to be used in all Scala projects in my company. It is a collection of commonly used SBT plugins and a common configuration that can be shared everywhere. My plugin uses e.g sbt-release: configures credentials, sets a release process, a default publish repository, etc. So I added it as a plugin dependency
project/plugins.sbt:
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.7")
However I'd like to expose this plugin to my users, so they can call sbt release. Currently I have the following code:
build.sbt
sbtPlugin := true
/* common release configuration code */
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.7")
If I remove the last line, my user cannot call sbt release. However I want to avoid declaring the dependency twice. Can I do better?
I went with a different approach, as the configurations didn't even get applied this way, and it also solves the duplication issue. Instead of sbt files, I created Scala files for the plugin and included the required plugin dependencies in build.sbt. This way you don't need to add it to project/plugins.sbt at all.
Example:
build.sbt
sbtPlugin := true
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.7")
src/main/scala/mycompany/MyReleaseConfigPlugin.scala
package mycompany
import sbt._
import Keys._
object MyReleaseConfigPlugin extends AutoPlugin {
override def trigger: PluginTrigger = allRequirements
override def requires = sbtrelease.ReleasePlugin
/* common release configuration code example */
import sbtrelease.ReleasePlugin.autoImport._
import ReleaseTransformations._
override lazy val projectSettings = Seq(
releaseProcess := Seq[ReleaseStep](
checkSnapshotDependencies,
inquireVersions,
runClean,
setReleaseVersion,
commitReleaseVersion,
tagRelease,
publishArtifacts),
releaseIgnoreUntrackedFiles := false,
publishMavenStyle := true,
credentials += Credentials(
"Artifactory Realm",
"artifactory.mycompany.com",
sys.env.get("REPO_USER").getOrElse(""),
sys.env.get("REPO_PASS").getOrElse(""))
)
}

scala reference sbt project version

I have a scala play application; I am trying to expose health check for our service. Part of the health check I would like to capture the project artifact version.
Can I know how I can reference project artifact version from health check controller in play application. We are making use of sbt for the build.
sample sbt file
import root.sbt.Keys._
import com.typesafe.sbt.SbtNativePackager._
import NativePackagerKeys._
import play.PlayScala
name := "artifact-name"
version := "0.5"
scalaVersion := "2.11.1"
javacOptions ++= Seq("-source", "1.7", "-target", "1.7")
scalacOptions += "-target:jvm-1.7"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
The xsbt-reflect plugin gives you access to the SBT variables at runtime.
All you need to do inside of your health route is return
Reflect.version

How to make import org.scalacheck.Gen possible in console/Scala REPL?

I'm trying to run ScalaCheck on REPL.
So I made an sbt project with the following build.sbt:
name := "Trying out ScalaCheck"
version := "1.0"
scalaVersion := "2.11.2"
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.11.5" % "test"
resolvers +=
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
Then, I typed reload and updated to get the library.
Finally, after running console, I tried to import Gen via:
scala> import org.scalacheck.Gen
<console>:7: error: object scalacheck is not a member of package org
import org.scalacheck.Gen
^
Looking at the docs, I'm not sure why I can't perform this import.
You added Scalacheck to the test scope, so it is available only within it. You should use test:console sbt command instead. See sbt Scopes docs for details.

Unable to add scala-reflect as a dependency

I can't add scala-reflect as dependency. My project/build.sbt looks like this:
//the name of the project, will become the name of the war file when you run the package command.
name := "Test-SBT"
version := "1.0"
//specify which Scala version we are using in this project.
scalaVersion := "2.10.3"
libraryDependencies <++= (scalaVersion)(sv =>
Seq(
"org.scala-lang" % "scala-reflect" % "2.10.3",
"org.scala-lang" % "scala-compiler" % "2.10.3"
)
)
And project/build.properties
sbt.version=0.13.0
And here is my Main class:
object Main1 extends App {
import scala.reflect.runtime.universe
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
//......
}
It says object runtime is not a member of package reflect. Of course, I did "gen-idea", "clean" and other things. What's up with that?
Guessing here due the question by #laughedelic.
The build.sbt should be in the root. Assuming the project you are writing is in test-sbt, you should end up with structure similar to:
test-sbt/build.sbt
test-sbt/project
Otherwise the build.sbt is used in creating the "internal compile project" used by SBT.
A deeper explanation can be found at SBT's docs sbt is recursive.

Explain Build.scala in play framework

Can somebody explain the syntax for Build.scala in detail? For example, I have the following Build.scala:
import sbt._
import Keys._
import play.Project._
object Build extends sbt.Build {
val appName = "myapp"
val appVersion = "1.0"
val appDependencies = Seq(
"postgresql" % "postgresql" % "9.1-901-1.jdbc4",
javaCore,
javaJdbc,
javaEbean,
"org.json" %"org.json" % "chargebee-1.0",
"org.reflections" % "reflections" % "0.9.8",
"org.mockito" % "mockito-all" % "1.9.5" % "test"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
libraryDependencies += "com.jolbox" % "bonecp" % "0.8.0-rc2-SNAPSHOT",
resolvers += Resolver.url("sbt-plugin-snapshots", new URL("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots/"))(Resolver.ivyStylePatterns),
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
)
}
How do I match the appDependencies with the resolvers? Or how do I know what resolvers I need to add for what appDependencies? Where do I look in the resolver's repo? What is libraryDependencies? How are they different from appDependencies?
Thanks.
There are a few things we need to disentangle here.
Let's first distinguish between an sbt build file and Build.scala. While the build file is exactly like a Maven pom or a Gradle build file, think of Build.scala as a build capability with the full power and expressiveness of Scala because it is a Scala class like any other. I believe the gap between the two has narrowed though with the latest version of sbt.
Now in both sbt and Build.scala, you have the notion of library dependencies, which are the jar libraries containing code you can use for your projects. These libraries can be found in lots of places--Maven repositories, the local file system, etc. You use resolvers to specify those locations.
So you tell both which jars you need and where to find them.
As for appDependencies, that's not actually a thing. As I said, Build.scala is a class like any other, and appDependencies is just a variable name. It just makes sense to use that name because that Seq is what you will pass to the Project constructor.