frazman
frazman

Reputation: 33223

How to do unit testing in Scala

I am trying to learn scala (and also the concept of unit testing).

I have an object

object Foo{
          def parse(s:String): Array[String] = {
              return s.split(",")
           }

       }

A very simple code block.. but now I want to write unit test?

My code structure is:

src/main/scala/foo.scala
   src/test/scala/(empty)

I am using sbt to compile and run?

Thanks

Upvotes: 1

Views: 384

Answers (2)

Jean
Jean

Reputation: 21595

put this in src/test/scala/FooSpec.scala

import org.specs2.mutable.Specification

class FooSpec extends Specification {
  "Foo" should {
    "parse a String" in {
      Foo.parse("a,b") == Array("a","b")      
    }
  }
}

then in the sbt prompt you can run test

for this to work you will need to add a dependency on specs 2 in your build.sbt as explained in the documentation

libraryDependencies ++= Seq(
    "org.specs2" %% "specs2" % "2.3.11" % "test"
)

Upvotes: 3

Randall Schulz
Randall Schulz

Reputation: 26486

It's a very big topic.

I'm a proponent of Specs2 along with its Mockito and ScalaCheck support. All of these have good documentation, so I recommend you start by looking them up on the Web.

Upvotes: 2

Related Questions