All IT Courses 50% Off
QA Tutorials

Assertion Testing

Assertion Testing is a Boolean expression at a specific point in a program which is considered as true unless there is a bug in the program. A test assertion is explained as an expression, which encapsulates some testable logic specified about a target under test.

How assertions block in testing?

If an assertion is failing due to one or the other reason, the consequence of the same which can be severe. An assertion cloud elevate to a stumbling block which might result in stopping testing for whole day.

The main benefits of assertions are identifying defects in a program. The uses of assertions are listed below

  • It is used to detect errors which might get unnoticed
  • It may be used for detecting errors quicker after they occur.
  •  Make statement about the effects of the code that is guaranteed to be true.

What is JUnit Asserts?

Assert is known as a method which is useful in evaluating pass/fail status of a test case, the assert methods are provided by the class org.junit.Assert which extends java.lang.object class.

The Assert methods that are provided by the class org.junit.Assert which extends java.lang.object class.

JUnit Assert methods

  1. Boolean

If we want to test the Boolean conditions you can use following assert methods.

  • AssertTrue(condition)
  • assertFalse(condition)

Here the condition is a Boolean value.

  1. Null object

If you want to check the initial value of any object/variable we have below methods

  • assertNull(object)
  • assertNotNull(object)

Object is java object e.g assertNull(actual)

  1. Identical

If we want to check whether objects are identical and also comparing two references to the same java object and may be different.

  • AssertSame (expeted, actual), it will return true if expected == actual.
  • assertNotSame(expected,actual)
  1. Assert Equals

If we want to test equality of two objects we have following methods

  • assertEquals(expected,actual)

It always returns true if expected. equals (actual) returns true.

  1. Assert Array Equals

If you want to test equality of arrays we have following methods

  • arrayArrayEquals(expected,actual)

here above method must be used if arrays have the same length for each valid value for ‘i’ we can check it is given 

  • assertEquals(expected[i],actual[i])
  • assertArrayEquals(expected[i],actual[i])
  1. Fail Message

If we want to throw any assertion error, we have to use fail() that  always results in a verdict.

  • Fail(message);

We will have an assertion testing method with additional String parameter a the first parameter. This string will be appended in failure message if an assertion fails e.g fail(message) can also be written as

asserEquals(message, expected,actual)

JUnit Assert Example

Let us create a simple test class name JUnit4AssertionTest.java and also test runner class TestRunner.java

In this example we will execute our test class using TestRunner.java

Step 1: Consider creating a class covering all important assert statement methods of Junit

package JKUnit.junit;
 
import static org.junit.Assert.*;
import org.junit.Test;
 
 
public class JunitAssertionTest {
 
    @Test
    public void testAssert(){
        
        //Variable declaration
        String string1="Junit";
        String string2="Junit";
        String string3="test";
        String string4="test";
        String string5=null;
        int variable1=1;
        int variable2=2;
        int[] airthematicArrary1 = { 1, 2, 3 };
        int[] airthematicArrary2 = { 1, 2, 3 };
        
        //Assert statements
        assertEquals(string1,string2);
        assertSame(string3, string4);
        assertNotSame(string1, string3);
        assertNotNull(string1);
        assertNull(string5);
        assertTrue(variable1<variable2);
        assertArrayEquals(airthematicArrary1, airthematicArrary2);
    }
}
 
Step 2: create a TestRunner.java class to execute the above code
package JKUnit.junit;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
      Result result = JUnitCore.runClasses(JunitAssertionTest.class);
for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println("Result=="+result.wasSuccessful());
   }

consider all assert statements test one by one see that if they returns true.

step 4: Right click on JunitAssertion.java and click runAs->Junit the output is obtained.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Related Articles

Check Also
Close
Back to top button
Close
Close

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.