Created
October 24, 2010 12:47
-
-
Save chbaranowski/643512 to your computer and use it in GitHub Desktop.
Beispiel Test SWQS Komponententests (Modultests) und Testabdeckung - Übung3 TDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import java.util.HashMap; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class TextParserTest { | |
TextParser parser; | |
HashMap<String, Integer> expedtedWordCounts; | |
@Before | |
public void setup(){ | |
parser = new TextParser(); | |
expedtedWordCounts = new HashMap<String, Integer>(); | |
} | |
// 1. TDD Cycle - Der Test wurde aus Aufgabenstellung abgeleitet | |
// die erste einfachste Implementierung die Komponente TextParser liefert die erwatete Map. | |
@Test | |
public void testTextWordCount_manyWords() throws Exception { | |
// Beispiel String aus der Aufgabenstellung | |
String text = "Hallo hallo Beispiel TEXT für text"; | |
// Tabelle aus der Aufgabenstellung modelliert als Map | |
expedtedWordCounts.put("Hallo", 2); | |
expedtedWordCounts.put("Beispiel", 1); | |
expedtedWordCounts.put("Text", 2); | |
expedtedWordCounts.put("Für", 1); | |
// Test | |
assertEquals(expedtedWordCounts, parser.parseTextWordCount(text)); | |
} | |
// 2. TDD Cycle - Damit die Komponente TextParse auch mit anderen Texten umgehen kann, | |
// wird ein zweiter Test formuliert, anschließend wird die Implementierung mit String[] words = text.split(" "); implementiert. | |
@Test | |
public void testTextWordCount_OneWord() throws Exception { | |
String text = "Hallo"; | |
expedtedWordCounts.put("Hallo", 1); | |
assertEquals(expedtedWordCounts, parser.parseTextWordCount(text)); | |
} | |
// 3. TDD Cycle - Hinzufügen Test der prüft das bei Whitespace eine leere Map geliefert wird, | |
// anschließende Implementierung über die Java 6 String Funktion text.isEmpty() | |
@Test | |
public void testTextWordCount_EmptyText() throws Exception { | |
String text = " "; | |
assertEquals(expedtedWordCounts, parser.parseTextWordCount(text)); | |
} | |
// 4. TDD Cycle - Hinzufügen von Test der prüft das bei Whitespace als Text eine leere Map geliefert. | |
// anschließende Implementierung über das IF-Statement if(text != null && !text.isEmpty()) | |
@Test | |
public void testTextWordCount_Null() throws Exception { | |
String text = null; | |
assertEquals(expedtedWordCounts, parser.parseTextWordCount(text)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment