Scala – JUnit

Below is the basic implementation of Test Case and Test Suite using JUnit (Java Unit Test Style) in Scala.

Note:  Test Cases and Test Suite should be placed in the package (src\test\scala).

 

build.sbt (Dependencies)

 

For Simple JUnit Tests from IDE

libraryDependencies ++= Seq(

“junit” % “junit” % “4.8.1” % “test”
)

 

For Running SBT Tests ($ sbt test)

libraryDependencies ++= Seq(

“com.novocode” % “junit-interface” % “0.8” % “test->default”
)

 

 

StartApp.scala

package com.cisco.cand.app

 

object startApp extends App {

 

def fullName (firstName: String, lastName: String): String = {

“Hello, ” + firstName + ” ” + lastName + “!”
}

 

}

 

StartAppTestCase1.scala

import com.cisco.cand.app.startApp

import org.junit.Assert._

import org.junit.Test
class StartAppTestCase1 {

 

@Test
def equalsTest(): Unit ={

assertEquals(“Compare method output”, “Hello, Arvind Gudiseva!”, startApp.fullName (“Arvind”, “Gudiseva”))

}

 

@Test
def trueTest(): Unit ={

val resultStr: String = startApp.fullName (“Arvind”, “Gudiseva”)

assertTrue(“Matching method output”, resultStr.equalsIgnoreCase(“Hello, Arvind Gudiseva!”))

}

 

@Test
def falseTest(): Unit ={

val resultStr: String = startApp.fullName (“Arvind”, “Gudiseva”)

assertFalse(“Mismatch of method output”, resultStr.equalsIgnoreCase(“Hi, Arvind Gudiseva!”))

}

 

}

 

StartAppTestCase2.scala

import com.cisco.cand.app.startApp

import org.junit.Assert._

import org.junit.Test
class StartAppTestCase2 {

 

@Test
def isEmptyFalse(): Unit ={

val resultStr: String = startApp.fullName (“”, “”)

assertFalse(“Result String should not be empty”, resultStr.isEmpty)

}

 

}

 

StartAppTestSuite.scala

import org.junit.runner.RunWith

import org.junit.runners.Suite
@RunWith(classOf[Suite])

@Suite.SuiteClasses(Array(classOf[StartAppTestCase1], classOf[StartAppTestCase2]))

class StartAppTestSuite{

// No code exists here

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s