Testing with Scala / SBT
Pre-Req :
Os - Linux ubuntu / debian
Java /jdk 1.8 +
scala 2.11 +
Intellij
Install sbt plugin in Intelli
Create an scala project in Intelli using sbt
Commands:
scala -version
sbt sbtVersion
----build.sbt (add below line)--------
scalaVersion := "2.xx.0"
libraryDependencies += "org.scalatest" % "scalatest_2.xx" % "3.0.8" % "test"
libraryDependencies +="org.pegdown" % "pegdown" % "1.4.2" % "test"
libraryDependencies += "org.mockito" % "mockito-all" % "1.8.4"
libraryDependencies += "org.scalamock" %% "scalamock" % "4.3.0" % "test"
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-h", "target/test-reports")
(click on auto-import link when it appears)
--- install sbt----
open terminal
echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt
---- Config sbt in IntelliJ----
to Debug/Run test in sbt:
1. Edit Configurations
2. "+" Add Scala Test
3. In the new file , Select the test file (test.scala)
4. Check "use sbt"
5. Apply and OK
6. rt ck on test file in File structure/ editor / tool bar - ck run/Debug
-----Run Sbt in terminal -----
cd to projects >
run command "sbt test"
---------------------
Src File : src/main/scala/sqCalculator.scala
object sqCalculator extends App {
def sq(x: Int) = {
x * x
}
}
Test File : test/scala/sqCalculatorTest.scala
import org.scalatest.FunSuite
class sqCalculatorTest extends FunSuite {
test("sqCalculator.cube") {
if(sqCalculator.sq(3) === 9){
println ("passed")
assert (true)
}
else{println (false)}
}
}
---------------------//Mockito----------------------------
//test/scala/LoginService.scala
trait LoginService {
def login(name: String, password: String): String
}
//test/scala/LoginServiceTest.scala
import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import org.scalatestplus.mockito.MockitoSugar
import org.mockito.Mockito._
class LoginServiceTest extends FunSuite with BeforeAndAfter with MockitoSugar {
before {
println("Start")
}
after {
println("Ends")
}
test ("test login service"){
val service = mock[LoginService]
when(service.login("johndoe", "secret")).thenReturn(("johndoe"))
when(service.login("joehacker", "secret")).thenReturn("an")
val johndoe = service.login("johndoe", "secret")
val joehacker = service.login("joehacker", "secret")
if(johndoe == "johndoe"){
assert(true)
}
if(joehacker == None)
assert(false)
else
assert(true)
}
}
----------------ScalaMock--------------------------------
//test/scala/LoginServiceTest2.scala
import org.scalatest.{BeforeAndAfter, FunSpec, FunSuite}
import org.scalamock.scalatest.MockFactory
class LoginServiceTest2 extends FunSpec with BeforeAndAfter with MockFactory {
before{
println("LoginServiceTest2 - start")
}
it("val test") {
val any_name: LoginService = mock[LoginService]
(any_name.login _) expects("a","a") returning "hi"
println(any_name.login("a","a"))
}
after{
println("LoginServiceTest2 - end")
}
}