Junit And Mockito

package com.lec.test;
import java.util.ArrayList;
import java.util.List;
public class Math {
public boolean isPrime(int n){
boolean isPrime = true;
for(int i = 2 ; 2*i < n ; i++){
if(n%i == 0){
isPrime = false;
}
}
return isPrime;
}
public boolean isPerfect(int x) {
int sum = 0;
for(int i = 1; i<=(x/2); i ++) {
if(x%i == 0) {
sum = sum + i;
}
}
if(sum == x) {
return true;
}else {
return false;
}
}
public int[] bubbleSort(int[] num) {
int j;
boolean flag = true; // set flag to true to begin first pass
int temp; //holding variable
System.out.println("bubbleSort");
while (flag) {
flag = false; //set flag to false awaiting a possible swap
for (j = 0; j < num.length - 1; j++) {
if (num[j] < num[j + 1]) // change to > for ascending sort
{
temp = num[j]; //swap elements
num[j] = num[j + 1];
num[j + 1] = temp;
flag = true; //shows a swap occurred
}
}
}
return num;
}
public int[] selectionSort(int[] num) {
for (int i = 0; i < num.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < num.length; j++)
if (num[j] < num[index])
index = j;
int smallerNumber = num[index];
num[index] = num[i];
num[i] = smallerNumber;
}
return num;
}
public static int numZero(int[] x) {
// Effects: if x == null throw NullPointerException
// else return the number of occurrences of 0 in x
int count = 0;
for (int i = 1; i < x.length; i++) {
if (x[i] == 0) {
count++;
}
}
return count;
}
public int findLast(int[] x, int y) {
//Effects : if x = null throw nullPointerExceptions
//else return the index of the last elements
// in x that equals y
// if no such element exists, return -1
for(int i=x.length-1 ; i >0 ; i--) {
if(x[i] == y) {
return i;
}
}
return -1;
}
public List<String> getTodos(List<String> list) {
List<String> filteredList = new ArrayList<String>();
for (String topic : list) {
if (topic.contains("coverage")) {
filteredList.add(topic);
}
}
return filteredList;
}
}

  1. Design test cases for all operations in “Math” class

  2. Create the “math” instance(object) for testing by using “@Before” or “@BeforeEach” annotation.

  3. Write Junit testing code for each operation (@Test)

  4. Execute all test case with coverage and capture the screenshot of testing results

  5. The method percentage should be 100%

  6. The Line coverage should be more than 85%

  7. You can use “assertEquals” or “assertThat” type operation



IntelliJ

Junit And Mockito 1