Home » JVM Languages » Scala » MUnit Testing – Scala

About Bhagvan Kommadi

Avatar photo
Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.

MUnit Testing – Scala

1. Introduction

This is an article about Munit Testing in Scala. In scala, there are unit testing frameworks that help control the quality of the code. Unit tests can be run in the suite and regression testing can be done by running the suite. There are testing frameworks like ScalaTest and Munit which provide the features of test suite and execution.

2. MUnit Testing

2.1 Prerequisites

SBT is required on windows or any operating system.

2.2 Download

SBT can be downloaded from the website.

2.3 Setup

2.3.1 Scala Setup

To install scala, the download package or executable needs to be executed. To check the setup, let us try a sample code in scala as shown below.

Hello Class

object Hello{  
    def main(args:Array[String]){  
        print("Hello Scala \n")  
    }  
}  

Now let us run the code by compiling with scalac and executing the code with scala as shown below:

Hello Class Execution

(base) apples-MacBook-Air:munit_scala bhagvan.kommadi$ scalac Hello.scala
(base) apples-MacBook-Air:munit_scala bhagvan.kommadi$ scala Hello
Hello Scala 
(base) apples-MacBook-Air:munit_scala bhagvan.kommadi$

2.4. What is Munit?

Munit is better than Scala Test for unit testing scala programs. It is similar to Junit and easy for newcomers. No additional scala dependencies are required for cross-build. It has better diffs on assertion failures.

2.4.1 MUnit Testing

Let us start with an example scala code. Calculator has two methods – addNumbers, and multNumbers.

Calculator Class

package org.javacodegeeks.munit

object Calculator {

  def addNumbers(n1: Int, n2: Int): Int = {
    n1 + n2
  }
  def multNumbers(n1: Int, n2: Int): Int = {
    n1 * n2
  }

}

2.5 Example

Now let us write a unit test to test the two methods in the calculator Object.

Calculator Unit Test

package org.javacodegeeks.munit

import org.javacodegeeks.munit.Calculator

class CalculatorSpec extends munit.FunSuite {

  test("adds two numbers") {
    assertEquals(Calculator.addNumbers(4, 8), 12)
  }
  
  test("multiplies two numbers") {
    assertEquals(Calculator.multNumbers(5, 9), 45)
  }

  test("considers collections with the same elements equal") {
    assertEquals(Seq(4, 5), Seq(4, 5))
  }

  

}

2.6 Verifying Unit Test Results

Unit test results can be verified by using assert, assertEquals,assertNotEquals,intercept, and assertDiffUnit test results can be verified by creating tests as shown above in the code for add Numbers, multNumbers, and verifying two collections if they are equal. SBT is used for executing the project as shown below.

SBT Project Execution

(base) apples-MacBook-Air:munit_test bhagvan.kommadi$ sbt test
[info] Loading project definition from /Users/bhagvan.kommadi/OldDesk/JavacodeGeeks/Code/munit_scala/munit_test/project/project
[info] Loading settings for project munit_test-build from plugins.sbt ...
[info] Loading project definition from /Users/bhagvan.kommadi/OldDesk/JavacodeGeeks/Code/munit_scala/munit_test/project
[info] Loading settings for project munit_test from build.sbt ...
[info] Set current project to munit-test (in build file:/Users/bhagvan.kommadi/OldDesk/JavacodeGeeks/Code/munit_scala/munit_test/)
org.javacodegeeks.munit.CalculatorSpec:
  + adds two numbers 0.341s
  + multiplies two numbers 0.001s
  + considers collections with the same elements equal 0.004s
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[success] Total time: 6 s, completed Nov 4, 2022 12:36:18 AM

3. Download the Source Code

Download
You can download the full source code of this example here: Unit Testing – Scala

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments