Assert is commonly used in testing the functionality of a program. As an argument, the assert method must prove the condition that follows. This method is included in the predef Scala package; thus no other packages are required. In the article, we will understand the concept of assert in Scala. The assert method needs a condition to be satisfied when performing a specific action.
What is Assert in Scala?
The assert method in Scala is a way to dynamically verify the invariant. The assert method takes a Boolean expression as a parameter and checks it throughout the given code. If the Boolean expression is true, Scala’s assert will execute normally. If false, then Scala’s assert will fail with an AssertionError. There is another method defined by the Assertion trait which hides the one specified by predef Scala. It is the same as AssertionError but it throws a TestFailedException upon condition false. Scalatest assert gives an accurate message error than Scala’s assert.
How to Type Casting in Scala in Ubuntu 20.04?
ScalaTest provides three assertions by default. These are the following:
Assert: assert can be used to make general statements assertions.
AssertResult: To distinguish between expected and actual numbers.
Intercept: To verify that a piece of code throws is an expected exception.
To know how these methods will work in a Scala program, we should have some hands-on the below example. All the implementations are in Scala sbt build for scalatest assertion in the IntelliJ idea application.
Example # 1: Program of Assert Method in Scala
Assert is used to make general statements. We can write assertions in any Scala program by calling the “assert” keyword. Then, passing the Boolean expression in Scala’s assert method. Let’s have some hands-on assert methods in the Scala program.
In the above Scala program, we have class objects represented as “examples”. In the main method of “example”, we have defined variables as “val”. The variables are defined as “length” and “width” which are initialized with different numeric values. We have used the “assert” keyword for the assertion method. In Scala’s assert method, we have a Boolean expression as length is equal to the width. Scala’s assert method will return normally if the specified Boolean expression is true. If the given Boolean expression is false, then the assert method will fail with an AssertionError.
As we have different length and width values, so the condition is false. We have an AssertionError as Assertion failed. The exception is presented in the image below.
Example # 2: Program of AssertResult Method in Scala
The above example is unable to distinguish between the actual and expected values. As the operand “==” are simply termed. For this purpose, we have a method named assert result, that can be used against assert keywords. After using assertResult., we will pass the expected value in it. AssertResult should pass the expected value or else exceptions will occur.
In the overhead code, we have imported the “org.scalatest.Matchers” package from “org. scalatest”. This package is basically for scalatest assertion. Then, we have an object class as “assert1”. In the main method of assert1, we have defined two variables. The variable is storing the numeric values which are defined as “x” and “y”. We will print the values of “x” and “y” through the print statement. After that, we have the assertResult method which has an expected value of 4 as mentioned in the code. In the assertResult method, we are performing the subtraction operation on the variables “x” and “y”. If we get the value of “x-y” as the expected value, then the print statement will show the return value of “x-y” or else we will have a TestFailedException.
From the “x-y”, we have a return value “1” but “4” is the expected value. So, the output has a TestFailedException description as below.
In the above assertResult example code, we have an exception occur in the code because the “x-y” value returned is different from the expected value. Now, we know that the expected value results as x-y is equal to 1. We have passed the value “1” in the assertResult method to see what it will return. If the expected value is returned, only the print statement will print the “x-y” result.
As we have an expected value return as an output, so the code will execute normally.
Example # 3: Program of AssertThrows and Intercept Method in Scala
When a function is called with invalid input, we may need to check whether a function throws a predictable exception on a satisfying condition. We cannot do this in scalatest which provides two methods: The assertThrows and the intercept method.
In the overhead code, we have stated an object class as “assert1”. In the main method of the object class, we have defined a variable as “val”. The variable is defined as “a” and initialized with the string Scala. Then, we have called the assertThrows method in which we have the charAt method. We have passed the index number for a string Scala of variable an as -2. The assertThrows will return successfully. Upon execution, we have not got an exception and the output is printed in the below image.
The program is the same as shown below just using the intercept method.
The intercept function is similar to assertThrows, but instead of returning true, it returns the captured exception, which you can analyze further if desired. In the above code, we are verifying if the data inside the exception has the expected values. As we have given the expected value, so the execution of this code is normal as in the above code example of assertThrows.
The guide aims to give you a clear and concise understanding of scala’s assert and scalatest assertion. The scalatest assertion is needed to validate the program’s execution. We have demonstrated all three by default scalatest assertion along with the given example implementation and also show the exceptions upon the compilation time. These illustrations will assist you a lot in dealing with Scalatest Assertion.