Unit Testing, Test and Behaviour Driven development in Scala with JUnit

Overview

With the evolutionary growth of the software development process, Test Driven Development is an approach that consolidates test first driven methodology and refactoring.

Test Driven Development is a key practice for extreme programming, it suggests that the code is developed or changed exclusively on the basis of the Unit Testing.


TDD (Test Driven Development)

It is a practice of writing a (failing) test prior to writing the code of a feature. Feature code is refined until it passes the unit test.

Steps for the same are given below -


BDD (Behaviour Driven Development)

Behaviour Driven Development (BDD) is similar to the Test Driven Development. In other words, Behaviour Driven Development is the extended version of Test Driven Development.

The process is similar to Test Driven Development. In this also, the code is first written in Behaviour Driven Development and the production code. But, the main difference is that in Behaviour Driven Development the test is written in plain descriptive English type grammar as opposed to Test Driven Development.

This type of development -

The major benefit of Behaviour Driven Development is that it can easily be understood by a non-technical person also.


What is Unit Testing?

Unit tests operate on a low level and are tightly coupled with source code. In the best of worlds, one unit test always correlates to one minimal piece of functionality. These functionalities are independent of the other components of the code


What is Integration Testing?

Integration tests validate that software components work correctly when combined together. Only the interfaces between components are tested and it is assumed that each component has been individually unit tested.


Test Driven Development Workflow

Test Driven Development promotes the idea of each test case testing one piece of functionality at a time. The workflow of Test Driven Development is as follows - 


Test First Development in Agile


Test Driven Development

It is the practice of first writing a (failing) test prior to writing the code for a feature. Feature code is refined until it passes tests(s).

If the above condition is not satisfied, then it allows us to change the portion of design affected by new functionality and allows us to add new features very easily.

You May also Love to Read Test Driven Development in Java


Need For Test Driven Development


TDD Life Cycle


Test Driven Development with Scala

Scala is a programming language which combines both object-oriented and functional programming paradigms. Considering functional aspects of Scala we can define functions of two types -

In scala, tests reside in project source but separated from the application code. There are many many testing frameworks available with a lot of features, one can choose according to the specific features in the framework. We will consider ScalaTest, a popular testing framework, in this document.


TDD Style Unit Tests with ScalaTest

ScalaTest is a testing framework inspired by JUnit. ScalaTest supports different styles of writing test, each designed to address specific needs.

Some of the testing styles include - 

Every member of the team is supposed to follow the same chosen style.

Scala test recommends FlatSpec for the unit and integration testing and FeatureSpec for acceptance testing.


Setting Up Environment For TDD

Perform the following steps -


	|-- build.sbt
|-- lib
|-- project
|-- src
|   |-- main
|   |   |-- java
|   |   |-- resources
|   |   |-- scala
|   |-- test
|       |-- java
|       |-- resources
|       |-- scala
|-- target


Implementing Test Driven Development


import org.scalatest.FlatSpec
class PizzaTests extends FlatSpec {
 val pizza = new Pizza "A new pizza"
 should "have zero toppings" in {
  assert(pizza.getToppings.size == 0)
 }
 "A pizza"
 should "have one topping" in {
  pizza.addTopping(Topping("green olives"))
  assert(pizza.getToppings.size === 1)
 }
 it must "get pizza pricing"
 is(pending)
}


class Pizza {
 private val toppings = new ArrayBuffer[Topping]
 def addTopping(t: Topping) {}
 def removeTopping(t: Topping) {}
 def getToppings = toppings.toList
}

Output -


import scala.collection.mutable.ArrayBuffer
case class Topping(name: String)
class Pizza {
 private val toppings = new ArrayBuffer[Topping]
 def addTopping(t: Topping) {
  toppings += t
 }
 def removeTopping(t: Topping) {
  toppings -= t
 }
 def getToppings = toppings.toList
}

Output -


Summary

Test-driven development (TDD) is a development technique where the developer must first write a test that fails before one writes new functional code. TDD ensures a proven way to ensure effective unit testing however it does not replace traditional testing.

We believe that TDD is an incredible practice that all software developers should consider during the development process.


How Can Don Help You?

Don follows the Test Driven Development Approach in the development of Enterprise level Applications following Agile Scrum Methodology.

Apache Spark enables in-memory distributed datasets that optimize workloads in addition to interactive queries. Apache Spark uses Scala as its application framework for manipulating distributed datasets as locally collective objects. Deliver highly scalable and effective Big Data Analytics Application with production grade implementation. Realize your business potential with Big Data Analysis services and data-driven decision making. 

Harness Reactive Programming to build scalable and fault-tolerant distributed system using Scala and Akka Framework. Reactive Programming in Scala and Akka provides the platform to develop resilient, Internet Scale Applications. 

Build Scalable and Flexible Scala Applications using Play Framework and Akka Framework. Leverage our Scala Proficiency to bring your Scala Application to market. Deploy and Manage your Scala Application on Leading Cloud Service Providers like Google Cloud, Microsoft Azure, AWS, and on Container Environment - Docker and Kubernetes.