java - 通过JUNIT运行程序时如何打印值(用于调试)

标签 java eclipse debugging junit

我想在序言中说我很精通 python,但对 java 略知一二,对 JUNT 几乎不了解

在 python 中调试时,我通常会 print输出我认为可能错误的值 当通过 JUNIT 运行脚本时,我将如何在 java 中执行此操作?

更具体地说,我有一种方法可以将符号添加到 map来自某个字符串,如果它们出现多次,则 value key的在 map是递增的。

static Map<Character, Double> getCharFrequancies(String text){
    Map<Character, Double> myMap = new HashMap<Character, Double>();
    int len = (text.length())/2;
    for(int i = text.length() - 1; i>=0; --i) { 
        char symbol = text.charAt(i);
        if(myMap.containsKey(symbol)){
            myMap.put(symbol, myMap.get(symbol)+(1/len));
        }
        else {
            myMap.put(symbol, (double) 1);
        }

    }

    return myMap;
}

和测试脚本:

public void testGetCharFrequancies() {
    Map<Character,Double> expectedMap = new HashMap<Character,Double>();
    String text = "aa, b ccc.";
    expectedMap.put('a', 2/10.0);
    expectedMap.put(' ', 2/10.0);
    expectedMap.put(',', 1/10.0);
    expectedMap.put('b', 1/10.0);
    expectedMap.put('c', 3/10.0);
    expectedMap.put('.', 1/10.0);

    Map<Character,Double> actualMap = HuffmanTree.getCharFrequancies(text);
    assertEquals(expectedMap.size(), actualMap.size());
    assertEquals(expectedMap.keySet(), actualMap.keySet());
    for(Character c:expectedMap.keySet()){
        assertEquals(expectedMap.get(c), actualMap.get(c), 0.000000000001);

失败发生在 assertEquals(expectedMap.get(c), actualMap.get(c), 0.000000000001); 所以我想打印出values map的.我该怎么做?

ps。我正在使用 eclipse 氧气

最佳答案

我建议您在方法 getCharFrequencies 中按如下所示编写它。熟悉 JDK 8 中的新 lambda 会很有帮助

package utils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @author Michael
 * @link https://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856
 */
public class StringUtils {

    private StringUtils() {}

    public static List<String> tokenize(String str) {
        String [] tokens = new String[0];
        if (isNotBlankOrNull(str)) {
            str = str.trim();
            tokens = str.split("\\s+");
        }
        return Arrays.asList(tokens);
    }

    public static boolean isBlankOrNull(String s) {
        return ((s == null) || (s.trim().length() == 0));
    }

    public static boolean isNotBlankOrNull(String s) {
        return !isBlankOrNull(s);
    }

    public static boolean hasSufficientTokens(int numTokens, String str) {
        return (numTokens >= 0) && tokenize(str).size() >= numTokens;
    }

    public static Map<String, Long> getCharFrequencies(String text) {
        Map<String, Long> charFrequencies = new TreeMap<>();
        if (isNotBlankOrNull(text)) {
            // https://stackoverflow.com/questions/4363665/hashmap-implementation-to-count-the-occurences-of-each-character
            charFrequencies = Arrays.stream(text.split("")).collect(Collectors.groupingBy(c -> c, Collectors.counting()));
        }
        return charFrequencies;
    }
}

这是证明它有效的 JUnit 测试:

package utils;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Michael
 * Creation date 12/6/2016.
 * @link https://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856
 */
public class StringUtilsTest {

    @Test
    public void testIsNotBlankOrNull_NullString() {
        Assert.assertFalse(StringUtils.isNotBlankOrNull(null));
    }

    @Test
    public void testIsNotBlankOrNull_EmptyString() {
        Assert.assertFalse(StringUtils.isNotBlankOrNull(""));
    }

    @Test
    public void testIsNotBlankOrNull_BlankString() {
        Assert.assertFalse(StringUtils.isNotBlankOrNull("        "));
    }

    @Test
    public void testIsNotBlankOrNull_FullString() {
        Assert.assertTrue(StringUtils.isNotBlankOrNull("I'm not null, blank, or empty"));
    }

    @Test
    public void testTokenize_NullString() {
        // setup
        List<String> expected = Collections.EMPTY_LIST;
        // exercise
        List<String> actual = StringUtils.tokenize(null);
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testTokenize_EmptyString() {
        // setup
        List<String> expected = Collections.EMPTY_LIST;
        // exercise
        List<String> actual = StringUtils.tokenize("");
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testTokenize_BlankString() {
        // setup
        List<String> expected = Collections.EMPTY_LIST;
        // exercise
        List<String> actual = StringUtils.tokenize("        ");
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testTokenize_FullString() {
        // setup
        List<String> expected = Arrays.asList("I'm", "not", "null,", "blank,", "or", "empty");
        // exercise
        List<String> actual = StringUtils.tokenize("    I'm not     null,    blank, or empty    ");
        // assert
        Assert.assertEquals(expected.size(), actual.size());
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void hasSufficientTokens_NegativeTokens() {
        // setup
        int numTokens = -1;
        String str = "    I'm not     null,    blank, or empty    ";
        // exercise
        // assert
        Assert.assertFalse(StringUtils.hasSufficientTokens(numTokens, str));
    }

    @Test
    public void hasSufficientTokens_InsufficientTokens() {
        // setup
        String str = "    I'm not     null,    blank, or empty    ";
        int numTokens = StringUtils.tokenize(str).size() + 1;
        // exercise
        // assert
        Assert.assertFalse(StringUtils.hasSufficientTokens(numTokens, str));
    }

    @Test
    public void hasSufficientTokens_NullString() {
        // setup
        String str = "";
        int numTokens = StringUtils.tokenize(str).size();
        // exercise
        // assert
        Assert.assertTrue(StringUtils.hasSufficientTokens(numTokens, str));
    }

    @Test
    public void hasSufficientTokens_Success() {
        // setup
        String str = "    I'm not     null,    blank, or empty    ";
        int numTokens = StringUtils.tokenize(str).size();
        // exercise
        // assert
        Assert.assertTrue(StringUtils.hasSufficientTokens(numTokens, str));
    }

    @Test
    public void testGetCharFrequencies_NullText() {
        // setup
        String text = null;
        Map<String, Long> expected = new TreeMap<>();
        // exercise
        Map<String, Long> actual = StringUtils.getCharFrequencies(text);
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testGetCharFrequencies_BlankText() {
        // setup
        String text = "        ";
        Map<String, Long> expected = new TreeMap<>();
        // exercise
        Map<String, Long> actual = StringUtils.getCharFrequencies(text);
        // assert
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void testGetCharFrequencies_Success() {
        // setup
        String text = "The quick brown fox jumped over the lazy dog!        ";
        String expectedString = "{T=1,  =16, !=1, a=1, b=1, c=1, d=2, e=4, f=1, g=1, h=2, i=1, j=1, k=1, l=1, m=1, n=1, o=4, p=1, q=1, r=2, t=1, u=2, v=1, w=1, x=1, y=1, z=1}";
        // exercise
        Map<String, Long> actual = StringUtils.getCharFrequencies(text);
        // assert
        Assert.assertEquals(expectedString, actual.toString());
    }
}

关于java - 通过JUNIT运行程序时如何打印值(用于调试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665534/

相关文章:

java - Google Firebase 方法 onDataChange() 未调用

java - 将 native dll 与 jar 捆绑在一起

java - 总的单元测试执行时间消失了

java - 在eclipse插件中启动java程序失败

Java/NetBeans - 如何进入 jar 文件?

javascript - 如何找到刷新页面的代码?

java - 使用 onClickListener 时出错( Intent )

java - 如何通过拖放将 TreePanel 的根节点添加到另一个 TreePanel 中?

eclipse 自动完成

javascript - 在 IE 上调试 Javascript 的最佳方法是什么?