Tuesday, 3 July 2018

Junit: ErrorCollector: Continue test execution even after failure

ErrorCollector rule is used to collect all the incorrect results in a test case by continuing the test execution even after failure.

Find the below working application.

Arithmetic.java
package com.sample.arithmetic;

public class Arithmetic {

 public int add(int a, int b) {
  return a + b + b;
 }
}

ArithmeticTest.java
package com.sample.arithmetic;

import static org.hamcrest.CoreMatchers.equalTo;

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

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class ArithmeticTest {
 @Rule
 public final ErrorCollector collector = new ErrorCollector();

 @Test
 public void add_diffInputs() {
  Arithmetic arithmetic = new Arithmetic();

  collector.checkThat("2 + 3 is not equal to 5", 5, equalTo(arithmetic.add(2, 3)));
  collector.checkThat("20 + 30 is not equal to 50", 50, equalTo(arithmetic.add(20, 30)));
  collector.checkThat("200 + 300 is not equal to 500", 500, equalTo(arithmetic.add(200, 300)));

 }

}


Run ArithmeticTest.java, you can able to see below kind of error report.






Previous                                                 Next                                                 Home

No comments:

Post a Comment