JUnit Using Assertion - JUnit

How to use assettion in JUNIT?

Assertion

Here are the assertions are recorded in the Assert class.

Assertion class provides a set of assertion methods, useful for writing tests. Here only failed assertions are recorded. Let’s see 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 assertTrue(boolean condition)

Checks that a condition is true.

3

void assertFalse(boolean condition)

Checks that a condition is false.

4

void assertNotNull(Object object)

Checks that an object isn't null.

5

void assertNull(Object object)

Checks that an object is null.

6

void assertSame(boolean condition)

The assertSame() method tests if two object references point to the same object.

7

void assertNotSame(boolean condition)

The assertNotSame() method tests if two object references do not point to the same object.

8

void assertArrayEquals(expectedArray, resultArray);

The assertArrayEquals() method will test whether two arrays are equal to each other.

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

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

Let’s 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.

Verify the output.

Annotation

Annotations are known as meta-tags used to add your code, and apply them to methods or in class. These annotations in JUnit provide the following information about test methods −

  • Details of methods are going to run before and after test methods.
  • Details of methods run before and after all the methods, and.
  • Details of methods or classes will be ignored during the execution.

Below table provides a list of annotations and their meaning in JUnit −

Sr.No. Annotation & Description
1

@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

2

@Before

Several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before each Test method.

3

@After

If you allocate external resources in a Before method, you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method.

4

@BeforeClass

Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class.

5

@AfterClass

This will perform the method after all tests have finished. This can be used to perform clean-up activities.

6

@Ignore

The Ignore annotation is used to ignore the test and that test will not be executed.

Let’s create a java class file named JunitAnnotation.java in C:\>JUNIT_WORKSPACE to test annotation.

Now create a java class file named TestRunner.java in C:\>JUNIT_WORKSPACE to execute annotations.

Let’s compile the Test case and Test Runner classes using javac.

Run the Test Runner as per the defined in the provided Test Case class.

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JUnit Topics