Assertions in Junit

Junit has an in-built class called Assert, which contains a set of assertion methods used for writing tests to identify the failures.

Let’s explore the Assert statements of Junit

  1. assertEquals(String1,String2)

This method is used to compare two strings to check if they are equal.
a. If both the strings are equal, true value is returned
b. If both the strings are not equal, then false value is returned.

Example :

import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

public class assertEquals {
@Test
public void testAssertEquals() {

String ExpectString= “QACREATORS”;
String ActualString= “QACREATORS”;
assertEquals( ExpectString, ActualString);
}
}
OUTPUT :

Junit-AssertEquals-Example

2.assertSame(String3, string4):
assertsame method will check whether the two objects reference to the same object.

a.If both the object reference are equal, true boolean value is returned.
b.If both the object reference are not equal, false boolean value is returned.

example :

import org.junit.Test;
import static org.junit.Assert.*;

public class assertSame {
@Test
public void testAssertEquals() {

String ExpectString= “QACREATORS”;
String ActualString= “QACREATORS”;
assertSame( ExpectString, ActualString);
}
}
OUTPUT :
Junit-AssertSame-Example

3.assertNotSame(String1, string2):
assertNotsame method will check whether the two objects reference do not point to the same object.

a.If both the object reference are not equal, true boolean value is returned.
b.If both the object reference are equal, false boolean value is returned.

example :

import org.junit.Test;
import static org.junit.Assert.*;

public class assertNotSame {
@Test
public void testAssertNotSame() {

String ExpectString= “example”;
String ActualString= “QACREATORS”;
assertNotSame( ExpectString, ActualString);
}
}
OUTPUT :

Junit-AssertNotSame-Example

4.assertTrue(variable1==variable2)

assertTrue method will check if the condition variable1==variable2 satisifies.

a.If the condition satisified, true boolean value is returned.
b.If the condition doesnot satisifies, false boolean value is returned.

Example :

import org.junit.Test;
import static org.junit.Assert.*;

public class assertTrue {
@Test
public void testAssertTrue() {
String str1= “example”;
String str2= “QACREATORS”;
assertTrue( str1 == str2);
}
}
OUTPUT :

Junit-AssertTrue-Example

Junit-AssertTrue-Failure-example

5.assertFalse(variable1==variable2)
assertFalse method will check if the condition variable1==variable2 doesnot satisifies.

a.If the condition is not satisified, true boolean value is returned.
b.If the condition satisifies, false boolean value is returned.

Example :

import org.junit.Test;
import static org.junit.Assert.*;

public class assertFalse {
@Test
public void testAssertFalse() {

String str1= “example”;
String str2= “QACREATORS”;
assertFalse( str1 == str2);
}
}
OUTPUT :

Junit-AssertFalse-Example

6.assertNull(string1):

assertNull will check if the object is null.

If the string1 is equal to null, true value will be returned.
If the string1 is not equal to null, false value will be returned.

Example 1 :

import org.junit.Test;
import static org.junit.Assert.*;

public class assertNull {
@Test
public void testAssertNull() {
String str1= null;
assertNull( str1);
}
}
OUTPUT :

Junit-AssertNull-Example

Example 2 :

import org.junit.Test;
import static org.junit.Assert.*;

public class assertNull {
@Test
public void testAssertNull() {
String str2= “example”;
assertNull(str2);

}
}
OUTPUT :
Junit-AssertNull-Failure-Example

Junit-AssertNull-Failure-Message

7.assertNotNull(string1):

assertNotNull will check if the object is not null.

If the string1 is not equal to null, true value will be returned.
If the string1 is equal to null, false value will be returned.

import org.junit.Test;
import static org.junit.Assert.*;

public class assertNotNull {
@Test
public void testAssertNotNull() {

String str2= “example”;
assertNotNull(str2);

}
}
OUTPUT :

Leave a Comment

Your email address will not be published.