Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过JUNIT运行程序时如何打印值(用于调试)_Java_Eclipse_Debugging_Junit - Fatal编程技术网

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

Java 通过JUNIT运行程序时如何打印值(用于调试),java,eclipse,debugging,junit,Java,Eclipse,Debugging,Junit,我想在开场白中说,我对python非常精通,但对java只有一点了解,对JUNT几乎没有 在python中调试时,我通常会打印出我认为可能错误的值,在java中通过JUNIT运行脚本时,我该怎么做 更具体地说,我有一种方法,可以从某个字符串向映射添加符号,如果符号出现多次,则映射中键的值将递增 static Map<Character, Double> getCharFrequancies(String text){ Map<Character, Double>

我想在开场白中说,我对python非常精通,但对java只有一点了解,对JUNT几乎没有

在python中调试时,我通常会
打印出我认为可能错误的值,在java中通过JUNIT运行脚本时,我该怎么做

更具体地说,我有一种方法,可以从某个字符串向
映射添加符号,如果符号出现多次,则
映射中
将递增

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;
}
静态映射GetCharFrequencies(字符串文本){
Map myMap=newhashmap();
int len=(text.length())/2;
对于(int i=text.length()-1;i>=0;--i){
字符符号=text.charAt(i);
if(myMap.containsKey(符号)){
myMap.put(symbol,myMap.get(symbol)+(1/len));
}
否则{
myMap.put(符号,(双)1);
}
}
返回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);
public void testGetCharFrequencies(){
Map expectedMap=newhashmap();
String text=“aa,b和ccc。”;
预期MAP.put('a',2/10.0);
预期的MAP.put(“”,2/10.0);
预期地图放置(','1/10.0);
预期MAP.put('b',1/10.0);
预期MAP.put('c',3/10.0);
预期MAP.put('.',1/10.0);
Map actualMap=HuffmanTree.getCharFrequencies(文本);
assertEquals(expectedMap.size()、actualMap.size());
assertEquals(expectedMap.keySet(),actualMap.keySet());
for(字符c:expectedMap.keySet()){
资产质量(预期地图获取(c),实际地图获取(c),0.000000000001);
失败发生在
assertEquals(expectedMap.get(c),actualMap.get(c),0.000000000001);
所以我想打印
映射的
我该怎么做


另外,我正在使用eclipse oxygen

我建议您按照下面的方法
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;
    }
}
package-utils;
导入java.util.array;
导入java.util.List;
导入java.util.Map;
导入java.util.TreeMap;
导入java.util.stream.collector;
/**
*@作者迈克尔
*@linkhttps://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856
*/
公共类StringUtils{
私有StringUtils(){}
公共静态列表标记化(字符串str){
String[]标记=新字符串[0];
如果(isNotBlankOrNull(str)){
str=str.trim();
令牌=str.split(\\s+);
}
返回数组.asList(令牌);
}
公共静态布尔值isBlankOrNull(字符串s){
返回((s==null)| |(s.trim().length()==0));
}
公共静态布尔值isNotBlankOrNull(字符串s){
返回!isBlankOrNull(s);
}
公共静态布尔hasufficientTokens(int numTokens,String str){
return(numTokens>=0)和&tokenize(str).size()>=numTokens;
}
公共静态映射getCharFrequencies(字符串文本){
Map charFrequencies=newtreemap();
如果(不是空白或完整(文本)){
// https://stackoverflow.com/questions/4363665/hashmap-implementation-to-count-the-occurences-of-each-character
charFrequencies=Arrays.stream(text.split(“”).collect(Collectors.groupby(c->c,Collectors.counting());
}
返回频率;
}
}
下面是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());
    }
}
package-utils;
导入org.junit.Assert;
导入org.junit.Test;
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
导入java.util.Map;
导入java.util.TreeMap;
/**
*迈克尔创作的
*创建日期2016年6月12日。
*@linkhttps://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856
*/
公共类StringUtilTest{
@试验
public void testIsNotBlankOrNull_NullString(){
Assert.assertFalse(StringUtils.isNotBlankOrNull(null));
}
@试验
公共无效测试Lankornull_EmptyString(){
Assert.assertFalse(StringUtils.isNotBlankOrNull(“”);
}
@试验
public void testIsNotBlankOrNull_BlankString(){
Assert.assertFalse(StringUtils.isNotBlankOrNull(“”);
}
@试验
public void testistsnotblankornull_FullString(){
Assert.assertTrue(StringUtils.isNotBlankOrNull(“我不是null、blank或空”);
}
@试验
public void testTokenize_NullString(){
//设置
预期列表=集合。空的\u列表;
//练习
List-actual=StringUtils.tokenize(null);
//断言
Assert.assertEquals(预期、实际);
}
@试验
public void testTokenize_EmptyString(){
//设置
预期列表=集合。空的\u列表;
//练习
List actual=StringUtils.tokenize(“”);
//断言
Assert.assertEquals(预期、实际);
}
@试验
public void testTokenize_BlankString(){
//设置
预期列表=集合。空的\u列表;
//练习
List actual=StringUtils.tokenize(“”);
//断言
Assert.assertEquals(预期、实际);
}
@试验
public void testTokenize_FullString(){
//设置
列表应为Arrays.asList(“我”、“不是”、“空”、“空白”、“或”、“空”);
//练习
List actual=StringUtils.tokenize(“我不是null、空白或空”);
//断言
Assert.assertEquals(预期的.size(),实际的.size());
Assert.assertEquals(预期、实际);
}
@试验
public void hasufficientTokens_NegativeTokens(){
//设置
int numTokens=-1;
String str=“我不是空的、空的或空的”;
//练习
//断言
Assert.assertFalse(StringUtils.hasufficientTokens(numTokens,s