How to write JUnit test code in Java to find the Nth Fibonacci number in the series?

This is the JUnit 5 solution to cover all kinds of inputs provided to find the nth Fibonacci number in the series. This Unit test executes tests for the ‘RdInput’ function available in the class ‘NthFibonacciJ8’.

package pack1;

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 JUnitTestNthFibonacciJ8 {

	NthFibonacci_Using_CoreLogic NFUCL;
	NthFibonacci_Using_Recursion NFUR;
	NthFibonacci_Using_StreamAPI NFUSA;

	@BeforeEach
	void setUp() throws Exception {

		NFUCL = new NthFibonacci_Using_CoreLogic();
		NFUR = new NthFibonacci_Using_Recursion();
		NFUSA = new NthFibonacci_Using_StreamAPI();
	}

	@Test
	@DisplayName("Positive inputs - Exp value and Actual matches")
	void test1() {
		Assertions.assertEquals(34, NFUCL.RdInput(10));
		Assertions.assertEquals(55, NFUR.RdInput(11));
		Assertions.assertEquals(21, NFUSA.RdInput(9));
	}

	@Test
	@DisplayName("Negative inputs - Exp value and Actual not matches")
	void test2() {
		Assertions.assertNotEquals(63, NFUCL.RdInput(10));
		Assertions.assertNotEquals(21, NFUR.RdInput(11));
		Assertions.assertNotEquals(8, NFUSA.RdInput(9));
	}

	@Test
	@DisplayName("When the input is 0 - Exp value and Actual matches")
	void test3() {
		Assertions.assertEquals(0, NFUCL.RdInput(0));
		Assertions.assertEquals(0, NFUR.RdInput(0));
		Assertions.assertEquals(0, NFUSA.RdInput(0));
	}

	@Test
	@DisplayName("When the input is 1 - Exp value and Actual matches")
	void test4() {
		Assertions.assertEquals(1, NFUCL.RdInput(1));
		Assertions.assertEquals(0, NFUR.RdInput(1));
		Assertions.assertEquals(1, NFUSA.RdInput(1));
	}

	@Test
	@DisplayName("Negative inputs - Exp value and Actual not matches")
	void test5() {
		Assertions.assertEquals(0, NFUCL.RdInput(-7));
		Assertions.assertEquals(0, NFUR.RdInput(-14));
		Assertions.assertEquals(0, NFUSA.RdInput(-43));
	}

	@Test
	@DisplayName("Null inputs - Exp value and Actual matches")
	void test6() {
		Assertions.assertEquals(0, NFUCL.RdInput(null));
		Assertions.assertEquals(0, NFUR.RdInput(null));
		Assertions.assertEquals(0, NFUSA.RdInput(null));
	}
}
Output:
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
The given number is 10
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 10 is 34

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
The given number is 11
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 11 is 55

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
The given number is 9
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 9 is 21

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
The given number is 10
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 10 is 34

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
The given number is 11
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 11 is 55

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
The given number is 9
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 9 is 21

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
The given number is 0
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 0  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
The given number is 0
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: The given Input number is null
Nth Fibonacci of 0  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
The given number is 0
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
The given number is 1
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 1 is 1

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
The given number is 1
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of 1  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
The given number is 1
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
The given number is -7
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: The given Input number is null
Nth Fibonacci of -7  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
The given number is -14
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: The given Input number is null
Nth Fibonacci of -14  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
The given number is -43
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: The provided Input validated Successfully
Nth Fibonacci of -43  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_CoreLogic RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_CoreLogic'
Nth Fibonacci of null  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_Recursion RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_Recursion'
Nth Fibonacci of null  not exists

Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: This method 'RdInput' called from the Class 'pack1.JUnitTestNthFibonacciJ8'
Apr 16, 2023 2:33:25 PM pack1.ReadInputNthNum validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.NthFibonacci_Using_StreamAPI'
Apr 16, 2023 2:33:25 PM pack1.NthFibonacci_Using_StreamAPI RdInput
INFO: The given Input number is null
Nth Fibonacci of null  not exists