Wednesday, 27 June 2018

Junit: Aggregating tests in a suite

You can combine multiple test classes in a suite.

How to combine multiple classes in a suite?
By using @SuiteClasses annotation, you can combine the test classes.

How to run the test suite?
Annotate the suit class with below annotation, and run the class as junit test.
@RunWith(Suite.class)

For example, I created ArithmeticTestSuite by combining ArithmeticTest, FactorialTest, FibonacciTest classes like below.




Find the below working application.

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;
 }
 
 public int add(int a, int b) {
  return a +b;
 }
 
 public int sub(int a, int b) {
  return a-b;
 }
}

Factorial.java

package com.sample.arithmetic;

public class Factorial {

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

  int result = 1;

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

  return result;
 }
}

Fibonacci.java

package com.sample.arithmetic;

public class Fibonacci {

 public int fibNumber(int n) {
  if (n < 0)
   throw new IllegalArgumentException("Can't calculate fiboaaci for negative numbers");
  return fib(n);

 }

 private int fib(int n) {
  if (n == 1 || n == 0)
   return 1;
  return fib(n - 1) + fib(n - 2);
 }

}

ArithmeticTest.java

package com.sample.arithmetic;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class ArithmeticTest {

 @Test
 public void divide_10By2_Five() {

  Arithmetic obj1 = new Arithmetic();

  int actual = obj1.divide(10, 2);
  int expected = 5;

  assertEquals(expected, actual);

 }
}

FactorialTest.java

package com.sample.arithmetic;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class FactorialTest {

 @Test
 public void factorial_5_120() {
  Factorial factorial = new Factorial();
  
  int actual = factorial.factorial(5);
  int expected = 120;
  
  assertEquals(expected, actual);
  
  
 }

}


FibonacciTest.java

package com.sample.arithmetic;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * Test cases follow below naming convention. methodName_input_output format.
 * 
 * @author krishna
 *
 */
public class FibonacciTest {

 @Test
 public void Fibonacci_7_21() {
  Fibonacci fib = new Fibonacci();
  int actual = fib.fibNumber(7);
  int expected = 21;

  assertEquals(expected, actual);

 }
}

ArithmeticTestSuite.java

package com.sample.arithmetic;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ ArithmeticTest.class, FactorialTest.class, FibonacciTest.class })
public class ArithmeticTestSuite {

}


Previous                                                 Next                                                 Home

No comments:

Post a Comment