in JUnit by

Assert Class

Following is the declaration for org.junit.Assert class −

public class Assert extends java.lang.Object

This class provides a set of assertion methods useful for writing tests. Only failed assertions are recorded. Some of the important methods of Assert class are as follows −

Sr.No. Methods & Description

void assertEquals(boolean expected, boolean actual)

Checks that two primitives/objects are equal.


void assertFalse(boolean condition)

Checks that a condition is false.


void assertNotNull(Object object)

Checks that an object isn't null.


void assertNull(Object object)

Checks that an object is null.


void assertTrue(boolean condition)

Checks that a condition is true.


void fail()

Fails a test with no message.

Let's use some of the above-mentioned methods in an example. Create a java class file named in C:\>JUNIT_WORKSPACE.

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

public class TestJunit1 {
   public void testAdd() {
      //test data
      int num = 5;
      String temp = null;
      String str = "Junit is working fine";

      //check for equality
      assertEquals("Junit is working fine", str);
      //check for false condition
      assertFalse(num > 6);

      //check for not null value

Next, create a java class file named in C:\>JUNIT_WORKSPACE to execute test case(s).

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner1 {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit1.class);
      for (Failure failure : result.getFailures()) {

Compile the test case and Test Runner classes using javac.


Now run the Test Runner, which will run the test case defined in the provided Test Case class.

C:\JUNIT_WORKSPACE>java TestRunner1

Verify the output.

Learn More with Madanswer

Related questions

0 votes
0 votes
0 votes
asked May 18, 2020 in JUnit by GeorgeBell
0 votes