Нужно придумать тест для метода

 
 
 
Сообщения:4
Необходимо написать тест для метода getWords(). По заданию нужно списки для сравнения перевести в массив, и сравнить их с помощью Assert

Loader.java
---------------------------------------------
import java.nio.file.Files;
import java.nio.file.Paths;

public class Loader
{
public static void main(String[] args) throws Exception
{
//Reading file to the String
String text = new String(Files.readAllBytes(Paths.get("res/text_01.txt")));

TextAnalyzer analyzer = new TextAnalyzer(text);
System.out.println("Most frequent word: " + analyzer.getMostFrequentWord());
}


}
----------------------------------------------
TestTextAnalyzer.java
----------------------------------------------
import junit.framework.Assert;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import static org.junit.Assert.*;

/**
* Created by Programmer on 27.08.2018.
*/
public class TestTextAnalyzer {
@Test
public void getWordsTest()
{
String s1 = "this word occurs in The text more than another word";
TextAnalyzer textAnalyzer1 = new TextAnalyzer(s1);
ArrayList<String> words1 = textAnalyzer1.getWords();
String[] masWords1 = new String[words1.size()];
for(String mas1: words1)
{
int i = 0;
masWords1[i] = mas1;
}
String s2 = "This word occurs in the text MOre than another word.";
TextAnalyzer textAnalyzer2 = new TextAnalyzer(s2);
ArrayList<String> words2 = textAnalyzer2.getWords();
String[] masWords2 = new String[words2.size()];
System.out.println();
for(String mas2: words2)
{
int i = 0;
masWords2[i] = mas2;
}
assertArrayEquals(masWords1,masWords2);
}

@Test(expected = NullPointerException.class)
public void getWordsTestException()
{
TextAnalyzer textAnalyzer = new TextAnalyzer(null);
ArrayList<String> words = textAnalyzer.getWords();
Assert.assertEquals(words, null);

}

@Test
public void getMostFrequentWordTest() throws IOException
{
TextAnalyzer textAnalyzer = new TextAnalyzer("This word occurs in the text more than another word.");
String s1 = textAnalyzer.getMostFrequentWord();
String s2 = "word";
Assert.assertEquals(s2, s1);
}

@Test(expected = NullPointerException.class)
public void testNullPointerException() throws IOException
{
TextAnalyzer textAnalyzer = new TextAnalyzer(null);
String s1 = textAnalyzer.getMostFrequentWord();
String s2 = null;
Assert.assertEquals(s2, s1);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testIndexOutOfBoundsException() {
TextAnalyzer textAnalyzer = new TextAnalyzer("Это слово, лучшее слово");
String s1 = textAnalyzer.getMostFrequentWord();
String s2 = "слово";
Assert.assertEquals(s2, s1);
}
}
-------------------------------------------------------------
TextAnalyzer.java
-------------------------------------------------------------
import java.util.*;

public class TextAnalyzer
{
private String text;

public TextAnalyzer(String text)
{
this.text = text;
}

/**
* Splits the text into words
* @return
*/
public ArrayList<String> getWords()
{
String fragments[] = splitTextIntoFragments();
ArrayList<String> words = new ArrayList<>();
for(String fragment : fragments)
{
if (fragment.matches(".*[a-zA-Z].*")) {
words.add(fragment);
}
}
return words;
}

public String getMostFrequentWord()
{
ArrayList<String> words = getWords();
TreeMap<String, Integer> wordCounts = new TreeMap<>();
for(String word : words)
{
Integer count = wordCounts.get(word);
wordCounts.put(word, count == null ? 1 : count + 1);
}
Collections.sort(words, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return wordCounts.get(o2) - wordCounts.get(o1);
}
});
return words.get(0);
}

private String[] splitTextIntoFragments()
{
return text.toLowerCase().split("[^a-z0-9\\-']");
}
}
 
 
Сообщения:4
мой getWordsTest() написан неправильно.
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет