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

时间:2017-08-13 22:47:07

标签: 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的{​​{1}}。我该怎么做?

PS。我正在使用日食氧气

1 个答案:

答案 0 :(得分:1)

我建议您按照下面getCharFrequencies方法编写它。熟悉JDK 8中的新lambdas

会很好
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());
    }
}