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 ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.