This is the JUnit 5 solution to cover all kinds of inputs provided to find the duplicate words function. This Unit test executes tests for the ‘DuplicateWordsCheck’ function available in the class ‘DuplicateWordsJ8’.
package pack1;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class JunitTestDuplicateWordsJ8 {
InputSentence inputSentence;
Duplicatewords_Using_String DWUStrg;
Duplicatewords_Using_Hashmap DWUHmp;
Duplicatewords_Using_Collection DWUCfy;
@BeforeEach
void setUp() throws Exception {
inputSentence = new InputSentence();
DWUStrg = new Duplicatewords_Using_String();
DWUHmp = new Duplicatewords_Using_Hashmap();
DWUCfy = new Duplicatewords_Using_Collection();
}
@AfterEach
void tearDown() throws Exception {
}
@Test
@DisplayName("No repeats inputs should work")
final void test1() {
DWUStrg.DuplicateWordsCheck("No repeated words here");
Assert.assertTrue(DWUStrg.fetchDuplicateStatusFlag());
DWUHmp.DuplicateWordsCheck("Here also no repeats");
Assert.assertTrue(DWUHmp.fetchDuplicateStatusFlag());
DWUCfy.DuplicateWordsCheck("Same too nothing repeats");
Assert.assertTrue(DWUCfy.fetchDuplicateStatusFlag());
}
@Test
@DisplayName("Repeated words inputs should work")
final void test2() {
DWUStrg.DuplicateWordsCheck("repeated word repeated");
Assert.assertFalse(DWUStrg.fetchDuplicateStatusFlag());
DWUHmp.DuplicateWordsCheck("some words some times repeated");
Assert.assertFalse(DWUHmp.fetchDuplicateStatusFlag());
DWUCfy.DuplicateWordsCheck("was is not is");
Assert.assertFalse(DWUCfy.fetchDuplicateStatusFlag());
}
@Test
@DisplayName("Empty inputs should not be considered")
final void test3() {
DWUStrg.DuplicateWordsCheck("");
Assert.assertTrue(DWUStrg.fetchDuplicateStatusFlag());
DWUHmp.DuplicateWordsCheck("");
Assert.assertTrue(DWUHmp.fetchDuplicateStatusFlag());
DWUCfy.DuplicateWordsCheck("");
Assert.assertTrue(DWUCfy.fetchDuplicateStatusFlag());
}
@Test
@DisplayName("null inputs should not be considered")
final void test4() {
DWUStrg.DuplicateWordsCheck(null);
Assert.assertFalse(DWUStrg.fetchDuplicateStatusFlag());
DWUHmp.DuplicateWordsCheck(null);
Assert.assertFalse(DWUHmp.fetchDuplicateStatusFlag());
DWUCfy.DuplicateWordsCheck(null);
Assert.assertFalse(DWUCfy.fetchDuplicateStatusFlag());
}
}
Output:
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_String DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_String'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = 'No repeated words here'
No Duplicate words found in 'No repeated words here'
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Hashmap DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Hashmap'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = 'Here also no repeats'
HashMap of words from the given input sentence
{no=1, repeats=1, Here=1, also=1}
No Duplicate words found in 'Here also no repeats'
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Collection DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Collection'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = 'Same too nothing repeats'
No Duplicate words found in 'Same too nothing repeats'
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_String DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_String'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = 'repeated word repeated'
The word=repeated is repeated
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Hashmap DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Hashmap'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = 'some words some times repeated'
HashMap of words from the given input sentence
{some=2, times=1, words=1, repeated=1}
The word=some is repeated 2 times
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Collection DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Collection'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = 'was is not is'
The word=is repeated 2 times
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_String DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_String'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = ''
No Duplicate words found in ''
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Hashmap DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Hashmap'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
The given input sentence is = ''
HashMap of words from the given input sentence
{=1}
No Duplicate words found in ''
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Collection DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Collection'
The given input sentence is = ''
No Duplicate words found in ''
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: Input sentence is validated Successfully
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_String DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_String'
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_String DuplicateWordsCheck
INFO: The given Input sentence is null
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Hashmap DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Hashmap'
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Hashmap DuplicateWordsCheck
INFO: The given Input sentence is null
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Collection DuplicateWordsCheck
INFO: This method 'DuplicateWordsCheck' called from the Class 'pack1.JunitTestDuplicateWordsJ8'
Jan 20, 2023 9:14:55 PM pack1.InputSentence validateInputs
INFO: This method 'validateInputs()' called from the Class 'pack1.Duplicatewords_Using_Collection'
Jan 20, 2023 9:14:55 PM pack1.Duplicatewords_Using_Collection DuplicateWordsCheck
INFO: The given Input sentence is null


Leave a comment