Adding dependencies

Like any build tool, SBT allows you to resolve dependencies. SBT uses the Ivy / Maven2 patterns to resolve dependencies. So, if you are familiar with Maven2, Gradle, or Ant/Ivy, you will realize that setting SBT dependencies is the same, although with a different syntax. Dependencies are defined in the build.sbt file. There is no Scala development without unit tests. One of the most popular testing libraries is JUnit (http://junit. org/junit4/). JUnit works with Java and Scala projects. SBT will download and add JUnit to your Scala application classpath parameter. We need to edit the build.sbt file to add JUnit as a dependency as follows:

build.sbt

name := "hello-world-sbt"

version := "1.0" scalaVersion := "2.11.7" scalaVersion in ThisBuild := "2.11.7"

libraryDependencies += "junit" % "junit" % "4.12" % Test libraryDependencies += "com.novocode" % "junit-interface" % "0.11"

% "test"

testOptions += Tests.Argument(TestFrameworks.JUnit, "-q", "-v")

As I mentioned before, SBT uses the same pattern as Maven2 / Ivy with: group ID + artifactid + version. If you don't know the pattern for the library you want to add, you can check out the Maven Repository website (they generate SBT configs as well) at the

following link: http://mvnrepository . com/artifact/junit/junit/4. 12.

SBT has scope for dependencies. We don't want to ship JUnit as part of the source code dependency. That's why we have the % Test after the dependency definition.

Once you have saved the file with the new content, you can run $ sbt compile. SBT will download JUnit for you and store the jar into the local Ivy repo files located at /home/YOUR_USER/.ivy2/cache. With the dependency in place, we can add more code and also use SBT to run our tests as follows:

src/main/scala/calc.scala class Calculator{

def sum(a:Int,b:Int):Int = { return a + b

}

def multiply(a:Int,b:Int):Int = { return a * b

}

}

In the preceding code, we just created a simple and straightforward calculator in Scala, which can add two integer numbers and also perform multiplication of two integers numbers. Now we can move on to the unit tests using JUnit. Tests need to be located in the src/test/scala/ folder. Look at the following code:

src/test/scala/calcTest.scala

import org.junit.Test

import org.junit.Assert._

class CalcTest {

@Test

def testSumOK():Unit = {

val c:Calculator = new Calculator() val result:Int = c.sum(1,5) assertNotNull(c) assertEquals(6,result)

}

@Test

def testSum0():Unit = {

val c:Calculator = new Calculator() val result:Int = c.sum(0,0) assertNotNull(c) assertEquals(0,result)

}

@Test

def testMultiplyOk():Unit = {

val c:Calculator = new Calculator() val result:Int = c.multiply(2,3) assertNotNull(c) assertEquals(6,result)

}

@Test

def testMultiply0():Unit = {

val c:Calculator = new Calculator() val result:Int = c.multiply(5,0) assertNotNull(c) assertEquals(4,result)

}

}

Okay, now we can just run the tests with the command $ sbt test as follows:

As you can see in the previous screenshot, all the tests are running. A test is created when we add the Java annotation @Test, and it needs to be a public function as well. There is one test, called testMultiplyO , which fails, because it expects the result 4, but 5 multiplied by 0 is zero, so the test is wrong. Let's fix this method by changing assertion to accept zero, like in the following code, and rerun the $sbt test as follows:

@Test

def testMultiply0():Unit = {

val c:Calculator = new Calculator() val result:Int = c.multiply(5,0) assertNotNull(c) assertEquals(0,result)

}

Hooray! All the tests passed. By default, SBT runs all your tests in parallel, which is great for speeding up build time - nobody likes to wait when doing builds, and Scala is not the fastest tech to build. However, you can disable parallel tests if you want by adding the following line into the build.sbt:

parallelExecution in Test := false

 
Source