Thursday 28 June 2018

Junit: Assert Boolean values

Assert class provides below methods to check boolean values.

static public void assertTrue(String message, boolean condition)
static public void assertTrue(boolean condition)
static public void assertFalse(String message, boolean condition)
static public void assertFalse(boolean condition)

static public void assertTrue(String message, boolean condition)
Asserts that a condition is true. If the condition is false, then it fails the test case with given message

static public void assertTrue(boolean condition)
Asserts that a condition is true. If the condition is false, then it fails the test case without message

static public void assertFalse(String message, boolean condition)
Asserts that a condition is false. If the condition is true, then it fails the test case with given message.

static public void assertFalse(boolean condition)
Asserts that a condition is false. If the condition is true, then it fails the test case without message

Find the below working application.

package com.sample.arithmetic;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class DemoTestApp {

 @Test
 public void testAssertTrueWithMessage() {
  assertTrue("Condition should be true", true);
 }

 @Test
 public void testAssertTrueWithoutMessage() {
  assertTrue(true);
 }

 @Test
 public void testAssertFalseWithMessage() {
  assertFalse("Condition should be false", false);
 }

 @Test
 public void testAssertFalseWithoutMessage() {
  assertFalse(false);
 }
}

When you ran above test cases, you can see below kind of report in Eclipse.


Previous                                                 Next                                                 Home

No comments:

Post a Comment