How to write JUnit test code in Java for the function Reversing of each word from a given sentence ?

This is the JUnit 5 solution to cover all kinds of inputs provided to find the reversing each word from a given sentence. This Unit test executes tests for the ‘ParseEachWord’ function available in the class ‘SumofDgtsInAlphanumericJ8’.

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

	ReversingEachWord_Using_StringBuffer REWUStrB;
	ReversingEachWord_Using_String REWUStr;
	ReversingEachWord_Using_LambdaExp REWULamda;
	ReversingEachWord_Using_Stream REWUStream;

	@BeforeEach
	void setUp() throws Exception {
		REWUStrB = new ReversingEachWord_Using_StringBuffer();
		REWUStr = new ReversingEachWord_Using_String();
		REWULamda = new ReversingEachWord_Using_LambdaExp();
		REWUStream = new ReversingEachWord_Using_Stream();
	}

	@Test
	@DisplayName("Positive inputs - Exp value and Actual matches")
	void test1() {
		Assertions.assertEquals("4tinuJ dna 5tinuJ ", REWUStrB.ParseEachWord("Junit4 and Junit5").toString());
		Assertions.assertEquals("7avaJ dna 71avaJ ", REWUStr.ParseEachWord("Java7 and Java17").toString());
		Assertions.assertEquals("42.0.8LQSyM dna 02.0.8LQSyM ",
				REWULamda.ParseEachWord("MySQL8.0.24 and MySQL8.0.20").toString());
		Assertions.assertEquals("[8vtacmoT, dna, 9vtacmoT]",
				REWUStream.ParseEachWord("Tomcatv8 and Tomcatv9").toString());
	}

	@Test
	@DisplayName("Negative inputs  - Exp value and Actual does not matches")
	void test2() {
		Assertions.assertNotEquals("Junit4 dna 5tinuJ", REWUStrB.ParseEachWord("Junit4 and Junit5").toString());
		Assertions.assertNotEquals("Java17 dna 71avaJ ", REWUStr.ParseEachWord("Java7 and Java17").toString());
		Assertions.assertNotEquals("MySQL8.0.20 dna 02.0.8LQSyM ",
				REWULamda.ParseEachWord("MySQL8.0.24 and MySQL8.0.20").toString());
		Assertions.assertNotEquals("[Tomcatv10, dna, 9vtacmoT]",
				REWUStream.ParseEachWord("Tomcatv8 and Tomcatv9").toString());
	}

	@Test
	@DisplayName("Null inputs")
	void test3() {
		Assertions.assertEquals("", REWUStrB.ParseEachWord(null).toString());
		Assertions.assertEquals("", REWUStr.ParseEachWord(null).toString());
		Assertions.assertEquals("", REWULamda.ParseEachWord(null).toString());
		Assertions.assertEquals("[]", REWUStream.ParseEachWord(null).toString());
	}

	@Test
	@DisplayName("Empty inputs")
	void test4() {
		Assertions.assertEquals("", REWUStrB.ParseEachWord("").toString());
		Assertions.assertEquals(" ", REWUStr.ParseEachWord("").toString());
		Assertions.assertEquals("", REWULamda.ParseEachWord("").toString());
		Assertions.assertEquals("[]", REWUStream.ParseEachWord("").toString());
	}

	@Test
	@DisplayName("Input only a String - Exp value and Actual matches")
	void test5() {
		Assertions.assertNotEquals("evitisoP", REWUStrB.ParseEachWord("Positive").toString());
		Assertions.assertNotEquals("SoiranecS", REWUStr.ParseEachWord("ScenarioS").toString());
		Assertions.assertNotEquals("evitageN", REWULamda.ParseEachWord("Negative").toString());
		Assertions.assertNotEquals("stupnIetadilaV", REWUStream.ParseEachWord("ValidateInputs").toString());
	}

	@Test
	@DisplayName("Input only an Alphanumeric - Exp value and Actual matches")
	void test6() {
		Assertions.assertNotEquals("$nj$!($8J", REWUStrB.ParseEachWord("J8$(!$jn$").toString());
		Assertions.assertNotEquals("qA13~!|]{", REWUStr.ParseEachWord("{]|!~31Aq").toString());
		Assertions.assertNotEquals("Wa/`+57*", REWULamda.ParseEachWord("*75+`/aW").toString());
		Assertions.assertNotEquals("1@54L^*&X", REWUStream.ParseEachWord("X&*^L45@1").toString());
	}
}
Output:
Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Junit4 and Junit5'
This sentence 'Junit4 and Junit5' after reversing '4tinuJ dna 5tinuJ '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Java7 and Java17'
This sentence 'Java7 and Java17' after reversing '7avaJ dna 71avaJ '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'MySQL8.0.24 and MySQL8.0.20'
This sentence 'MySQL8.0.24 and MySQL8.0.20' after reversing '42.0.8LQSyM dna 02.0.8LQSyM '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Tomcatv8 and Tomcatv9'
This sentence 'Tomcatv8 and Tomcatv9' after reversing '[8vtacmoT, dna, 9vtacmoT]'


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Junit4 and Junit5'
This sentence 'Junit4 and Junit5' after reversing '4tinuJ dna 5tinuJ '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Java7 and Java17'
This sentence 'Java7 and Java17' after reversing '7avaJ dna 71avaJ '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'MySQL8.0.24 and MySQL8.0.20'
This sentence 'MySQL8.0.24 and MySQL8.0.20' after reversing '42.0.8LQSyM dna 02.0.8LQSyM '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Tomcatv8 and Tomcatv9'
This sentence 'Tomcatv8 and Tomcatv9' after reversing '[8vtacmoT, dna, 9vtacmoT]'


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing ''


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing ''


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing ''


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: The given Input sentence is null
This sentence 'null' after reversing '[]'


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing ''


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing ' '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing ''


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This sentence '' after reversing '[]'


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Positive'
This sentence 'Positive' after reversing 'evitisoP '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'ScenarioS'
This sentence 'ScenarioS' after reversing 'SoiranecS '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Negative'
This sentence 'Negative' after reversing 'evitageN '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'ValidateInputs'
This sentence 'ValidateInputs' after reversing '[stupnIetadilaV]'


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_StringBuffer ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_StringBuffer'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'J8$(!$jn$'
This sentence 'J8$(!$jn$' after reversing '$nj$!($8J '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_String'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = '{]|!~31Aq'
This sentence '{]|!~31Aq' after reversing 'qA13~!|]{ '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_LambdaExp ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_LambdaExp'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = '*75+`/aW'
This sentence '*75+`/aW' after reversing 'Wa/`+57* '


Feb 03, 2023 7:58:13 PM pack1.ReversingEachWord_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestReversingEachWordJ8'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.ReversingEachWord_Using_Stream'
The given input sentence is = 'X&*^L45@1'
Feb 03, 2023 7:58:13 PM pack1.InputSequenceOfStrings validateInputs
INFO: The provided Input validated Successfully
This sentence 'X&*^L45@1' after reversing '[1@54L^*&X]'