How to write JUnit test code in Java for the function Max and Min Length of Strings from a given sentence ?

This is the JUnit 5 solution to cover all kinds of inputs provided to find the Max and Min length of words from a given sentence. This Unit test executes tests for the ‘ParseEachWord’ function available in the class ‘MinMaxLengthStringJ8’.

package pack1;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.Assert;
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 JunitTestMinMaxLengthStringJ8 {

	MinMaxLenghtString_Using_String MMLSUS;
	MinMaxLenghtString_Using_Hashmap MMLSUHM;
	MinMaxLenghtString_Using_Stream MMLSUST;

	@BeforeEach
	void setUp() throws Exception {
		MMLSUS = new MinMaxLenghtString_Using_String();
		MMLSUHM = new MinMaxLenghtString_Using_Hashmap();
		MMLSUST = new MinMaxLenghtString_Using_Stream();
	}

	@Test
	@DisplayName("Positive inputs - Exp value and Actual matches")
	void test1() {
		String[] bothStrings = MMLSUS.ParseEachWord("Perfection is the Lowest Standard in the Life").split("#");
		assertEquals(bothStrings[0], "is");
		assertEquals(bothStrings[1], "Perfection");

		bothStrings = MMLSUHM.ParseEachWord("Optimal belief is not about Attitude but it's an Optimism").split("#");
		assertEquals(bothStrings[0], "an");
		assertEquals(bothStrings[1], "Optimism");

		bothStrings = MMLSUST.ParseEachWord("Resilience meant for how you recover from the problems").split("#");
		assertEquals(bothStrings[0], "the");
		assertEquals(bothStrings[1], "Resilience");
	}

	@Test
	@DisplayName("Positive inputs - Exp value and Actual matches - Array values")
	void test2() {
		String[] resultArray = MMLSUS.ParseEachWord("Perfection is the Lowest Standard in the Life").split("#");
		String[] expectedArray = { "is", "Perfection" };
		Assert.assertArrayEquals(expectedArray, resultArray);

		resultArray = MMLSUHM.ParseEachWord("Whoever is happy will make others happiest too").split("#");
		expectedArray = new String[] { "is", "happiest" };
		Assert.assertArrayEquals(expectedArray, resultArray);

		resultArray = MMLSUST.ParseEachWord("Best Quotes of All time").split("#");
		expectedArray = new String[] { "of", "Quotes" };
		Assert.assertArrayEquals(expectedArray, resultArray);
	}

	@Test
	@DisplayName("Negative inputs  - Exp value and Actual does not matches")
	void test3() {
		Assertions.assertNotEquals("lives#happy", MMLSUS.ParseEachWord("The purpose of our lives is to be happy"));
		Assertions.assertNotEquals("busy#dying", MMLSUHM.ParseEachWord("Get busy living or get busy dying"));
		Assertions.assertNotEquals("and#those", MMLSUST.ParseEachWord("Life has got all those twists and turns"));
	}

	@Test
	@DisplayName("Positive inputs  - Exp value and Actual does matches")
	void test4() {
		Assertions.assertEquals("and#outside", MMLSUS.ParseEachWord("walk outside and connect with nature"));
		Assertions.assertEquals("of#spreading", MMLSUHM.ParseEachWord("two ways of spreading light"));
		Assertions.assertEquals("day#masterpiece", MMLSUST.ParseEachWord("Make each day your masterpiece"));
	}

	@Test
	@DisplayName("Null inputs")
	void test5() {
		Assertions.assertEquals("null#null", MMLSUS.ParseEachWord(null));
		Assertions.assertEquals("null#null", MMLSUHM.ParseEachWord(null));
		Assertions.assertEquals("null#null", MMLSUST.ParseEachWord(null));
	}

	@Test
	@DisplayName("Empty inputs")
	void test6() {
		Assertions.assertEquals("null#null", MMLSUS.ParseEachWord(""));
		Assertions.assertEquals("null#null", MMLSUHM.ParseEachWord(""));
		Assertions.assertEquals("#", MMLSUST.ParseEachWord(""));
	}
}
Output:
Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
The given input sentence is = 'Perfection is the Lowest Standard in the Life'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_String'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
This Min Length String is 'is'& the Max Length String is 'Perfection'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Hashmap'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Optimal belief is not about Attitude but it's an Optimism'
This Min Length String is 'an'& the Max Length String is 'Optimism'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Stream'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Resilience meant for how you recover from the problems'
This Min Length String is 'the'& the Max Length String is 'Resilience'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_String'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Perfection is the Lowest Standard in the Life'
This Min Length String is 'is'& the Max Length String is 'Perfection'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Hashmap'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Whoever is happy will make others happiest too'
This Min Length String is 'is'& the Max Length String is 'happiest'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Stream'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Best Quotes of All time'
This Min Length String is 'of'& the Max Length String is 'Quotes'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_String'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'The purpose of our lives is to be happy'
This Min Length String is 'of'& the Max Length String is 'purpose'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Hashmap'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Get busy living or get busy dying'
This Min Length String is 'or'& the Max Length String is 'living'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Stream'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Life has got all those twists and turns'
This Min Length String is 'and'& the Max Length String is 'twists'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_String'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'walk outside and connect with nature'
This Min Length String is 'and'& the Max Length String is 'outside'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Hashmap'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'two ways of spreading light'
This Min Length String is 'of'& the Max Length String is 'spreading'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Stream'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = 'Make each day your masterpiece'
This Min Length String is 'day'& the Max Length String is 'masterpiece'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_String'
Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: The given Input sentence is null
This Min Length String is 'null'& the Max Length String is 'null'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Hashmap'
Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: The given Input sentence is null
This Min Length String is 'null'& the Max Length String is 'null'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Stream'
Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: The given Input sentence is null
This Min Length String is 'null'& the Max Length String is 'null'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_String ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_String'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This Min Length String is 'null'& the Max Length String is 'null'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Hashmap ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Hashmap'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This Min Length String is 'null'& the Max Length String is 'null'


Mar 04, 2023 8:29:55 PM pack1.MinMaxLenghtString_Using_Stream ParseEachWord
INFO: This method 'ParseEachWord' called from the Class 'pack1.JunitTestMinMaxLengthStringJ8'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.MinMaxLenghtString_Using_Stream'
Mar 04, 2023 8:29:55 PM pack1.SentenceInput validateInputs
INFO: The provided Input validated Successfully
The given input sentence is = ''
This Min Length String is ''& the Max Length String is ''


Comments

2 responses to “How to write JUnit test code in Java for the function Max and Min Length of Strings from a given sentence ?”

Leave a comment