Saturday 30 June 2018

Junit: Assumptions

Assumptions are very useful feature in junit. By using assumption, you can make sure that the test will not execute, if the preconditions/assumptions are not met. That means, the tests that do not satisfy the assumptions are skipped.

For example, below test case will execute in windows systems and skipped in other operating systems.

         @Test
         public void testCase1() {
                  assumeTrue(isWindows());
                  assertTrue("TestCase1 executed successfully", true);
         }

Find the below working application.

DemoTestApp.java
package com.sample.arithmetic;

import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
public class DemoTestApp {

 private static boolean isWindows() {
  String osType = System.getProperty("os.name");
  if (osType.contains("windows") || osType.contains("Windows") || osType.contains("WINDOWS")) {
   return true;
  }
  return false;
 }

 @Test
 public void testCase1() {
  assumeTrue(isWindows());
  assertTrue("TestCase1 executed successfully", true);
 }

 @Test
 public void testCase2() {
  assumeTrue(!isWindows());
  assertTrue("TestCase2 executed successfully", true);
 }

}

When I ran the tests in eclipse, I observed the testCase2 is skipped and it throws AssumptionViolatedException.

org.junit.Assume clas provides below methods to support different assumptions.


public static void assumeTrue(boolean b)
The test will be ignored, if the value of argument 'b' is false. When 'b' is false, junit throws AssumptionViolatedException.

public static void assumeTrue(String message, boolean b)
The test will be ignored, if the value of argument 'b' is false. When 'b' is false, junit throws AssumptionViolatedException with given message.

public static void assumeFalse(boolean b)
The test will be ignored, if the value of argument 'b' is true. When 'b' is false, junit throws AssumptionViolatedException.

public static void assumeFalse(String message, boolean b)
The test will be ignored, if the value of argument 'b' is true. When 'b' is false, junit throws AssumptionViolatedException with given message.

public static void assumeNotNull(Object... objects)
All the objects must not be null. If any of the objects is null, then the test is skipped and junit throws AssumptionViolatedException.

public static <T> void assumeThat(T actual, Matcher<T> matcher)
If the arguments 'actual' is not matched to the given matcher, then the test is skipped and junit throws AssumptionViolatedException.

public static <T> void assumeThat(String message, T actual, Matcher<T> matcher)
If the arguments 'actual' is not matched to the given matcher, then the test is skipped and junit throws AssumptionViolatedException with given message.

public static void assumeNoException(Throwable e)
It skips the tests on error cases, by throwing AssumptionViolatedException.

public static void assumeNoException(String message, Throwable e)
It skip the tests on error cases,  by throwing AssumptionViolatedException with given message.

Example of assumeNoException
In the below example, junit skip the test, if any exception is thrown by factorial() method.

         @Test
         public void testFactorial_12() {
                  Factorial fact = new Factorial();
                  long actual = -1;
                 
                  try {
                           actual = fact.factorial(12);
                  } catch (Exception e) {
                           assumeNoException(e);
                  }
                 
                  long expected = 479001600;
                 
                  assertEquals("Factorial of the number is not matched", expected, actual);
                 
         }

Find the below working example.
Factorial.java

package com.sample.arithmetic;

public class Factorial {

 public long factorial(int num) throws Exception {
  if (num < 0) {
   throw new IllegalArgumentException("Factorial is not computed for negative numbers");
  }

  if(num > 10) {
   throw new Exception("Can't calculate factorial for big number");
  }
  
  long result = 1;

  for (int i = 2; i <= num; i++) {
   result *= i;
  }

  return result;
 }
}


FactorialTest.java

package com.smaple.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeNoException;

import org.junit.Test;

import com.sample.arithmetic.Factorial;

public class FactorialTest {

 @Test
 public void testFactorial_5() {
  Factorial fact = new Factorial();
  long actual = -1;

  try {
   actual = fact.factorial(5);
  } catch (Exception e) {
   assumeNoException(e);
  }

  long expected = 120;

  assertEquals("Factorial of the number is not matched", expected, actual);

 }

 @Test
 public void testFactorial_12() {
  Factorial fact = new Factorial();
  long actual = -1;

  try {
   actual = fact.factorial(12);
  } catch (Exception e) {
   assumeNoException(e);
  }

  long expected = 479001600;

  assertEquals("Factorial of the number is not matched", expected, actual);

 }
}

When you ran FactorialTest.java, you can observe the test case ‘testFactorial_12’ is skipped.




Previous                                                 Next                                                 Home

No comments:

Post a Comment