O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using JUnit

Using JUnit to run tests written in Scala is really simple. Since Scala compiles to Java bytecode, you can write your tests in Scala, use scalac to compile your code into bytecode, and then run your tests like you normally run JUnit test cases. Remember to include the Scala library in your classpath. Let’s look at an example of writing a JUnit test in Scala:

UnitTesting/UsingJUnit.scala
 
import​ java.util.ArrayList
 
import​ org.junit.Test
 
import​ org.junit.Assert._
 
 
class​ UsingJUnit {
 
@Test
 
def​ listAdd() {
 
val​ list = ​new​ ArrayList[​String​]
 
list.add(​"Milk"​)
 
list add ​"Sugar"
 
assertEquals(2, list.size)
 
}
 
}

We imported java.util.AraryList and then org.junit.Test. We also included all the methods of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required