From 0dcf1268610cd24bdd5049073d3d9eaa10d20f4b Mon Sep 17 00:00:00 2001 From: "alfred.samanga@gmail.com" Date: Wed, 14 Aug 2019 20:39:00 +0200 Subject: [PATCH] Added test code for counting words in a string in Java --- .../main/java/com/baeldung/string/README.md | 3 --- .../string/wordcount/WordCounter.java | 12 +--------- .../string/wordcount/WordCountUnitTest.java | 24 +++++++++++++++++++ 3 files changed, 25 insertions(+), 14 deletions(-) delete mode 100644 java-strings-3/src/main/java/com/baeldung/string/README.md create mode 100644 java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java diff --git a/java-strings-3/src/main/java/com/baeldung/string/README.md b/java-strings-3/src/main/java/com/baeldung/string/README.md deleted file mode 100644 index e02980e93f..0000000000 --- a/java-strings-3/src/main/java/com/baeldung/string/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This file exists to ensure this empty directory is committed in Git. - -Please remove this file when this directory is populated. \ No newline at end of file diff --git a/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java b/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java index 6b061c7925..1ee7e4a4ef 100644 --- a/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java +++ b/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java @@ -2,19 +2,9 @@ package com.baeldung.string.wordcount; import java.util.StringTokenizer; -/** - * Created by alfred on 14 August 2019 - */ public class WordCounter { public static void main(String[] args) { - //test string - String random = "Lorem%Ipsum is-simply dummy text."; - - System.out.printf("Using delimiter, word count: %d\n", countWordsUsingTokenizer(random)); - System.out.printf("Using no library method, word count: %d\n", countWordsManually(random)); - System.out.printf("Using punctuation regex, word count: %d\n", countWordsUsingRegex(random)); - System.out.printf("\n\n%s\n\n", random); - + } public static int countWordsUsingRegex(String arg) { diff --git a/java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java b/java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java new file mode 100644 index 0000000000..a8f6a37434 --- /dev/null +++ b/java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.string.wordcount; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +public class WordCountUnitTest { + private String string1 = "This is a test sentence with eight words"; + private String string2 = "This#is%a test sentence with eight words"; + + @Test + public void givenStringWith8Words_whenUsingRegexCount_ThenResultEqual8() { + assertThat(WordCounter.countWordsUsingRegex(string2) == 8).isTrue(); + } + + @Test + public void givenStringWith8Words_whenUsingManualMethod_ThenWordCountEqual8() { + assertThat(WordCounter.countWordsManually(string1) == 8).isTrue(); + } + + @Test + public void givenAStringWith8Words_whenUsingTokenizer_ThenWordCountEqual8() { + assertThat(WordCounter.countWordsUsingTokenizer(string1) == 8).isTrue(); + } +}