+1 vote
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
1

void assertEquals(boolean expected, boolean actual)

Checks that two primitives/objects are equal.

2

void assertFalse(boolean condition)

Checks that a condition is false.

3

void assertNotNull(Object object)

Checks that an object isn't null.

4

void assertNull(Object object)

Checks that an object is null.

5

void assertTrue(boolean condition)

Checks that a condition is true.

6

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 TestJunit1.java in C:\>JUNIT_WORKSPACE.

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

public class TestJunit1 {
   @Test
   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
      assertNotNull(temp);
   }
}

Next, create a java class file named TestRunner1.java 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()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
}  	

Compile the test case and Test Runner classes using javac.

C:\JUNIT_WORKSPACE>javac TestJunit1.java TestRunner1.java

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.

true

Related questions

+1 vote
+1 vote
+1 vote
asked May 18, 2020 in JUnit by GeorgeBell
+1 vote
+1 vote
asked May 18, 2020 in JUnit by GeorgeBell
...