Monday 2 July 2018

Junit: ExpectedException rule: check the method is throwing specific exception

By using @ExpectedException rule, you can verify that your code throws a specific exception with given message.

Find the below working example.

Arithmetic.java
package com.sample.arithmetic;

public class Arithmetic {

 public int divide(int a, int b) {
  if (b == 0) {
   throw new IllegalArgumentException("Division by zero is not supported");
  }

  return a / b;
 }
}

ArithmeticTest.java
package com.sample.arithmetic;


import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class ArithmeticTest {
 @Rule
 public ExpectedException expectedException = ExpectedException.none();

 @Test
 public void divide_10By0_IllegalArgumentException() {

  Arithmetic obj1 = new Arithmetic();

  expectedException.expect(IllegalArgumentException.class);
  expectedException.expectMessage("Division by zero is not supported");

  obj1.divide(10, 0);

 }
}


Run the application, you can see below kind of report.


expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Division by zero is not supported");
Above statements check that the code is throwing ‘IllegalArgumentException’ with the message "Division by zero is not supported".

Assumptions and ExpectedException rule
You should call assumption methods before setting the expectations of ExpectedException rule. By setting the assumption before ExpectedException rules, test will not execute if the assumptions are not met.

ArithmeticTest.java
package com.sample.arithmetic;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assume.assumeTrue;

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class ArithmeticTest {
 @Rule
 public ExpectedException expectedException = ExpectedException.none();

 @Test
 public void divide_10By0_IllegalArgumentException() {

  Arithmetic obj1 = new Arithmetic();

  assumeTrue(false);

  expectedException.expect(IllegalArgumentException.class);
  expectedException.expectMessage("Division by zero is not supported");

  obj1.divide(10, 0);

 }
}


When I ran ArithmeticTest.java, I observed that the test divide_10By_0_IllegalArgumentException() is skipped  from execution.

If you put assumptions after the ExpectedException rule, the test case is failed.

ArithmeticTest.java

package com.sample.arithmetic;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assume.assumeTrue;

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class ArithmeticTest {
 @Rule
 public ExpectedException expectedException = ExpectedException.none();

 @Test
 public void divide_10By0_IllegalArgumentException() {

  Arithmetic obj1 = new Arithmetic();

  expectedException.expect(IllegalArgumentException.class);
  expectedException.expectMessage("Division by zero is not supported");
  
  assumeTrue(false);

  obj1.divide(10, 0);

 }
}


When I ran ArithmeticTest.java, I observe that the test case is failed.




Previous                                                 Next                                                 Home

No comments:

Post a Comment