FAQs Data: 03: ScalaTest using IntelliJ IDEA IDE

This extends FAQs Data: 01: Getting started with Spark Scala on IntelliJ IDEA.

Step 1: Note the relevant test libraries in the pom.xml file.

ScalaTest on IntelliJ IDEA

ScalaTest on IntelliJ IDEA

scalatest-maven-plugin is used to run the tests & scalatest_* & specs2-* dependencies are added as shown below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>simple-spark</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>
    <description>My wonderfull scala app</description>
    <inceptionYear>2018</inceptionYear>
    <licenses>
        <license>
            <name>My License</name>
            <url>http://....</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.12.6</scala.version>
        <scala.compat.version>2.12</scala.compat.version>
        <spec2.version>4.12.12</spec2.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_${scala.compat.version}</artifactId>
            <version>3.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2-core_${scala.compat.version}</artifactId>
            <version>${spec2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.specs2</groupId>
            <artifactId>specs2-junit_${scala.compat.version}</artifactId>
            <version>${spec2.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <!-- see http://davidb.github.com/scala-maven-plugin -->
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>2.0.0</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <junitxml>.</junitxml>
                    <filereports>WDF TestSuite.txt</filereports>
                </configuration>
                <executions>
                    <execution>
                        <id>test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Step 2: Create a very simple “Calculator” class with two methods – multiply(..) & divide(…). Create the class in a new package “myapp” under “src/main/scala”

package myapp

class Calculator {
    def multiply (num1: Int, num2: Int) : Int = num1 * num2
    def divide (num1: Int, num2: Int) : Int = num1 / num2
}

JUnit Style

package myapp

import org.junit._

@Test
class MyTestJunitTest {

  @Test
  def testMultiplyByZero() {
    val calc = new Calculator
    assert(calc.multiply(45, 0) == 0)
    assert(calc.multiply(-45, 0) == 0)
    assert(calc.multiply(0, 0) == 0)
  }

  @Test
  def testMultiplyNonZero() {
    val calc = new Calculator
    assert(calc.multiply(9, 5) == 45)
    assert(calc.multiply(-3, 2) == -6)
    assert(calc.multiply(-3, -2) == 6)
  }

  @Test(expected = classOf[ArithmeticException])
  def testExceptionDivideByZero() {
    val calc = new Calculator
    calc.divide(6, 0)
    calc.divide(-6, 0)
    calc.divide(0, 0)
  }
}

ScalaTest Unit Testing Style

package myapp

import org.scalatest.funsuite.AnyFunSuite

class MyTestScalaTest extends AnyFunSuite {
  test("Multiply by 0 should return 0"){
    val calc = new Calculator
    assert(calc.multiply(45,0) == 0)
    assert(calc.multiply(-45,0) == 0)
    assert(calc.multiply(0,0) == 0)
  }

  test("Multiply by non zero should return non zero"){
    val calc = new Calculator
    assert(calc.multiply(9,5) == 45)
    assert(calc.multiply(-3,2) == -6)
    assert(calc.multiply(-3,-2) == 6)
  }

  test("Division by 0 should throw ArithmeticException") {
    val calc = new Calculator
    assertThrows[ArithmeticException] {
      calc.divide(6,0)
    }

    assertThrows[ArithmeticException] {
      calc.divide(-6,0)
    }

    assertThrows[ArithmeticException] {
      calc.divide(0,0)
    }
  }
}

ScalaTest FunSpec BDD Style

Behaviour Driven Development (BDD) style of testing.

package myapp

import org.scalatest.funspec.AnyFunSpec

class MyTestFunSpec extends AnyFunSpec {

  describe("A multiplication") {
    describe("When by 0") {
      it("should return a 0") {
        val calc = new Calculator
        assert(calc.multiply(45, 0) == 0)
        assert(calc.multiply(-45, 0) == 0)
        assert(calc.multiply(0, 0) == 0)
      }
    }

    describe("When by non-zero") {
      it("should return a non-zero") {
        val calc = new Calculator
        assert(calc.multiply(9, 5) == 45)
        assert(calc.multiply(-3, 2) == -6)
        assert(calc.multiply(-3, -2) == 6)
      }
    }

    it("should throw an ArithmeticException when divide by a zero") {
      val calc = new Calculator
      assertThrows[ArithmeticException] {
        calc.divide(6, 0)
      }

      assertThrows[ArithmeticException] {
        calc.divide(-6, 0)
      }

      assertThrows[ArithmeticException] {
        calc.divide(0, 0)
      }
    }
  }
}

ScalaTest FlatSpec BDD Style

Behaviour Driven Development (BDD) style of testing.

package myapp

import org.scalatest.flatspec.AnyFlatSpec

class MyTestFlatSpec extends AnyFlatSpec{

  "Multiplying by 0" should "return a 0" in {
    val calc = new Calculator
    assert(calc.multiply(45, 0) == 0)
    assert(calc.multiply(-45, 0) == 0)
    assert(calc.multiply(0, 0) == 0)
  }

  "Multiplying by non-zero" should "return a non-zero" in {
    val calc = new Calculator
    assert(calc.multiply(9, 5) == 45)
    assert(calc.multiply(-3, 2) == -6)
    assert(calc.multiply(-3, -2) == 6)
  }

  it should "throw an ArithmeticException when divide by 0" in {
    val calc = new Calculator
    assertThrows[ArithmeticException] {
      calc.divide(6, 0)
    }

    assertThrows[ArithmeticException] {
      calc.divide(-6, 0)
    }

    assertThrows[ArithmeticException] {
      calc.divide(0, 0)
    }
  }

}

There are more Specs like AnyFreeSpec, AnyFeatureSpec & AnyPropSpec.

Which style?

ScalaTest supports different styles of testing, each designed to address a particular set of needs. Refer to ScalaTest styles. It is recommended to use AnyFlatSpec style as the default choice, because it is flat, and familiar to most developers, but guides you into writing focused tests with descriptive, specification-style names.

(Visited 5 times, 1 visits today)

800+ Java & Big Data Interview Q&As

200+ Java & Big Data Tutorials

Top