This is the JUnit 5 solution to cover all kinds of inputs provided to find the nth Largest number function. This Unit test executes tests for the ‘nthLargest’ function available in the class ‘NthLargestJ8’.
package pack1;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class NthLargestJ8Junit5 {
NthLargest_Using_Arrays_Generics NthLUAG;
NthLargest_Using_ArrayList_Generic_Streams NthLUALGS;
@BeforeEach
void setUp() throws Exception {
NthLUAG = new NthLargest_Using_Arrays_Generics();
NthLUALGS= new NthLargest_Using_ArrayList_Generic_Streams();
}
@AfterEach
void tearDown() throws Exception {
}
@Test
@DisplayName("Positive inputs should work - Array Generics")
void test1() {
Integer[] intArray = { 34, 21, 45, 75, 62, 98, 13, 49 };
Double[] doubleArray = { 34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0 };
Assertions.assertEquals(49, NthLUAG.nthLargest(intArray,4));
Assertions.assertEquals(75.0, NthLUAG.nthLargest(doubleArray,2));
}
@Test
@DisplayName("Positive inputs should work - Array List Generics Streams")
void test2() {
ArrayList<String> StringSt = new ArrayList<>(
Arrays.asList("MIKE", "RUSS", "SANJAY", "VALLI", "JOHN", "RAMAA", "DAVE", "NAGENDRA"));
ArrayList<Double> DoubleSt = new ArrayList<>(Arrays.asList(34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0));
Assertions.assertEquals("NAGENDRA", NthLUALGS.nthLargest(StringSt,5));
Assertions.assertEquals(62.0, NthLUALGS.nthLargest(DoubleSt,3));
}
@Test
@DisplayName("Unavailalbe data inputs should work - Array Generics")
void test3() {
Integer[] intArray = { 34, 21, 45, 75, 62, 98, 13, 49 };
Double[] doubleArray = { 34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0 };
Assertions.assertNotEquals(499, NthLUAG.nthLargest(intArray,4));
Assertions.assertNotEquals(58.0, NthLUAG.nthLargest(doubleArray,2));
}
@Test
@DisplayName("Unavailalbe data inputs should work - Array List Generics Streams")
void test4() {
ArrayList<String> StringSt = new ArrayList<>(
Arrays.asList("MIKE", "RUSS", "SANJAY", "VALLI", "JOHN", "RAMAA", "DAVE", "NAGENDRA"));
ArrayList<Double> DoubleSt = new ArrayList<>(Arrays.asList(34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0));
Assertions.assertNotEquals("KUMAR", NthLUALGS.nthLargest(StringSt,5));
Assertions.assertNotEquals(85.0, NthLUALGS.nthLargest(DoubleSt,3));
}
@Test
@DisplayName("Unavailable index must throw ArrayList Generics IndexOutOfBounds Exception")
void test5() {
ArrayList<Double> DoubleSt = new ArrayList<>(Arrays.asList(34.0, 21.0, 45.0, 75.0, 62.0, 98.0, 13.0, 49.0));
Throwable exception = Assert.assertThrows(ArrayIndexOutOfBoundsException.class, () -> NthLUALGS.nthLargest(DoubleSt,18));
Assertions.assertEquals("-10", exception.getMessage());
}
@Test
@DisplayName("Unavailable index must throw Array Generics IndexOutOfBounds Exception")
void test6() {
ArrayList<String> StringSt = new ArrayList<>(
Arrays.asList("MIKE", "RUSS", "SANJAY", "VALLI", "JOHN", "RAMAA", "DAVE", "NAGENDRA"));
Throwable exception = Assert.assertThrows(ArrayIndexOutOfBoundsException.class, () -> NthLUALGS.nthLargest(StringSt,21));
Assert.assertEquals("-13", exception.getMessage());
}
@Test
@DisplayName("Unavailable index must throw Array Generics IndexOutOfBounds Exception")
public void test7() {
Integer[] intArray = { 34, 21, 45, 75, 62, 98, 13, 49 };
Throwable exception = Assert.assertThrows(ArrayIndexOutOfBoundsException.class, () -> NthLUAG.nthLargest(intArray,56));
Assert.assertEquals("-48", exception.getMessage());
}
}
Output:
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 4th largest entity in Array is=49
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 2th largest entity in Array is=75.0
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 5th Largest value in given stream= NAGENDRA
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 3th Largest value in given stream= 62.0
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 4th largest entity in Array is=49
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 2th largest entity in Array is=75.0
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 5th Largest value in given stream= NAGENDRA
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
The 3th Largest value in given stream= 62.0
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
WARNING: The Array index 18 is out of bound -10
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_ArrayList_Generic_Streams nthLargest
WARNING: The Array index 21 is out of bound -13
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
INFO: This method 'nthLargest' called from the Class 'pack1.NthLargestJ8Junit5'
Jan 15, 2023 6:53:20 AM pack1.NthLargest_Using_Arrays_Generics nthLargest
WARNING: The Array index 56 is out of bound -48


Leave a comment