Thursday 28 June 2018

Junit: @RunWith: Run the tests with this class

When a class is annotated with @RunWith or extends a class annotated with @RunWith, then JUnit will invoke the class by the test runner specified in the annotation.

For example, below snippet runs using Suite runner class.


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