I write 2 methods to count the number of vowels in the string as below.
1. Returns the count of the vowel characters in the String
2. Return the number of vowels in each word in the String as a List of Integers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public static void main(String[] args) { System.out.print("Enter your string here: "); Scanner reader = new Scanner(System.in); String inputValue = reader.nextLine().toLowerCase(); char[] chars = inputValue.toCharArray(); int counting = 0; for (char s : chars) { switch (s) { case 'a': case 'e': case 'i': case 'o': case 'u': counting++; break; default: } } // Returns the count of the vowel characters in the String System.out.println("Number of vowel characters in the string: " + counting); // Return the number of vowels in each word in the String as a List of Integers for (String grabString : inputValue.split(" +")) System.out.println(grabString + ": " + grabString.replaceAll("[^aeiou]", "").length()); reader.close(); } |
Results
Looks good.
So what can I do to test the above code and make sure it works?
The idea is making a very simple Unit test with JUnit (or TestNG) framework to test these code, so I have created 2 classes: Count vowel methods and Test class as below:
1. CountVowelCustomized.java
This class is includes 2 methods displayVowelInString() to display the total number of vowels in the String and displayVowelInWord() to split the String into the single word and count the vowel in each word. They all returned the value to verify in the test class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package CountVowel_Tests; import java.util.ArrayList; public class CountVowelCustomized { public static class CountVowelChars { String inputChars; void setInput(String sChars) { inputChars = sChars; } int displayVowelInString() { String inputCharsConverted = inputChars.toLowerCase(); char[] chars = inputCharsConverted.toCharArray(); int counting = 0; for (char s : chars) { switch (s) { case 'a': case 'e': case 'i': case 'o': case 'u': counting++; break; default: } } // Returns the count of the vowel characters in the String System.out.println("Number of vowel characters in the string: " + counting); return counting; } public ArrayList<Integer> displayVowelInWord() { ArrayList<Integer> numberVowels = new ArrayList<Integer>(); String inputCharsConverted = inputChars.toLowerCase(); // Split the string to single words String strArray[] = inputCharsConverted.split(" "); // Count the number of vowels for (int i = 0; i < strArray.length; i++) { int ArrayList = strArray[i].replaceAll("[^aeiou]", "").length(); // Add the number of vowels to the array list numberVowels.add(ArrayList); } System.out.println(numberVowels); // Return the number of vowels as list return (numberVowels); } } static class testCountVowelChars { public static void main(String args[]) { CountVowelChars s1 = new CountVowelChars(); // Set the input string s1.setInput("Sang Bui"); s1.displayVowelInString(); s1.displayVowelInWord(); } } } |
2. CountVowelTests.java
To test the above code I need to create a Unit test by using JUnit, there are 2 test methods as testVowelString() & testVowelWord()
I have changed the input value to “It should be good” and expectedVowel = 6 (all vowels in the String), expectedVowelNumber = Arrays.asList(1, 2, 1, 2).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package CountVowel_Tests; import java.util.Arrays; import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import CountVowel_Tests.CountVowelCustomized.CountVowelChars; public class CountVowelTests { CountVowelCustomized VowelChars; CountVowelChars s1 = new CountVowelChars(); @Before public void setUp(){ // Set the input string to verify s1.setInput("It should be good"); } @Test public void testVowelString(){ // Expected number of vowel number int expectedVowel = 6; // Verify the actual and expected results Assert.assertEquals(expectedVowel, s1.displayVowelInString()); } @Test public void testVowelWord(){ // Expected list of integers List<Integer> expectedVowelNumber = Arrays.asList(1, 2, 1, 2); // Actual list of integers List<Integer> actualVowelNumber = s1.displayVowelInWord(); // Verify the actual and expected results Assert.assertEquals(expectedVowelNumber, actualVowelNumber); } } |
Run the script and take a look at the results.
Now I want to check the failed status if the expected value and actual value is inconsistent by changing the expected vowels of the String “It should be good” to 9. It should be failed.
int expectedVowel = 9;
This is the basic code to get an overview, hope this help and could make you interested in this topic, keep it up and also take a look at the below links for more information.
“Unit testing, a testing technique using which individual modules are tested to determine if there are any issues by the developer himself. It is concerned with functional correctness of the standalone modules. The main aim is to isolate each unit of the system to identify, analyze and fix the defects.”
Good reads:
#1. http://tutorials.jenkov.com/java-unit-testing/index.html
#2: https://lostechies.com/derekgreer/2011/03/14/effective-tests-a-unit-test-example
One Comment