Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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_Junit - Fatal编程技术网

Java 理解这些JUnit测试结果

Java 理解这些JUnit测试结果,java,junit,Java,Junit,我正在为我的CS课程做一些练习,他们给我们做Junit测试,但是他们只告诉我们我们是否失败或通过。对我来说,输出/预期输出是令人愉快的 我得到的预期产出/产出如下: java.lang.AssertionError: expected <3143794514> but was <459133821> java.lang.AssertionError:应为,但为 我注意到该值也可以在测试代码中找到。不过,我还是个初学者。显然,adler32是通过校验和来检查错误的,但

我正在为我的CS课程做一些练习,他们给我们做Junit测试,但是他们只告诉我们我们是否失败或通过。对我来说,输出/预期输出是令人愉快的

我得到的预期产出/产出如下:

java.lang.AssertionError: expected <3143794514> but was <459133821> 
java.lang.AssertionError:应为,但为
我注意到该值也可以在测试代码中找到。不过,我还是个初学者。显然,adler32是通过校验和来检查错误的,但我不知道如何利用它。有没有办法让它显示更有意义的消息,让我知道我的代码出了什么问题

我要数一数字符串中的所有单词。这些测试能告诉我返回错误答案的输入/输出是什么吗

以下是JUnit类的示例:

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.io.*;
import java.util.zip.Adler32;

public class TestStringProblems {

    private static final int RUNS = 100000;
    private static final int SEED = 12345;
    private StringProblems sp = new StringProblems();

    @Test
    public void testCountWords() {
        BufferedReader br = null;
        Adler32 check = new Adler32();
        int count = 0;
        try {
            br = new BufferedReader(new FileReader("warandpeace.txt"));
            String line = br.readLine();
            while(line != null) {
                int words = sp.countWords(line.trim());
                count += words;
                check.update(words);
                line = br.readLine(); 
            }
        } catch(IOException e) { System.out.println("Error: " + e); assertTrue(false); }
        finally { try { br.close(); } catch(Exception e) { } }
        assertEquals(count, 562491); // number of words in War and Peace
        assertEquals(check.getValue(), 2309395892L); // checksum of word counts
    }

    @Test
    public void testRemoveDuplicates() {
        Adler32 check = new Adler32();
        java.util.Random rng = new java.util.Random(SEED);
        for(int i = 0; i < RUNS; i++) {
            StringBuilder sb = new StringBuilder();
            int len = rng.nextInt(500);
            for(int j = 0; j < len; j++) {
                char c = (char)(1 + rng.nextInt(50000));
                int rep = rng.nextInt(10) + 1;
                for(int k = 0; k < rep; k++) {
                    sb.append(c);
                }
            }
            check.update(sp.removeDuplicates(sb.toString()).getBytes());
        }
        assertEquals(check.getValue(), 459133821L);
    }


}
import static org.junit.Assert.*;
导入org.junit.After;
导入org.junit.Before;
导入org.junit.Test;
导入java.util.*;
导入java.io.*;
导入java.util.zip.Adler32;
公共类测试问题{
专用静态最终整数运行=100000;
私有静态最终整数种子=12345;
私有StringProblems sp=新StringProblems();
@试验
public void testCountWords(){
BufferedReader br=null;
Adler32检查=新的Adler32();
整数计数=0;
试一试{
br=新的BufferedReader(新的文件阅读器(“warandpeace.txt”);
String line=br.readLine();
while(行!=null){
int words=sp.countWords(line.trim());
计数+=单词;
检查、更新(文字);
line=br.readLine();
}
}catch(IOException e){System.out.println(“错误:+e);assertTrue(false);}
最后{try{br.close();}catch(异常e){}
assertEquals(count,562491);//战争与和平中的字数
assertEquals(check.getValue(),2309395892L);//字数校验和
}
@试验
public void testRemoveDuplicates(){
Adler32检查=新的Adler32();
java.util.Random rng=新的java.util.Random(种子);
for(int i=0;i
谢谢

public class StringProblems {

    public String removeDuplicates(String s) {
        String newStr = "";
        if (s.length() == 0) {
            return s;
        }

        int length = s.length() - 1;
        for(int i = 0;i<length+1;i++) {
            if(i!=0 && s.charAt(i)!=s.charAt(i-1)) {
                newStr += s.charAt(i);
            }
        }


        return s.charAt(0) + newStr;
    }

    public int countWords(String s) {
        String newStr = s.trim(); // removes unnecessary whitespace

        if (newStr.isEmpty()) {
            return 0;
        }

        return newStr.split("\\W+").length; // should work since it creates an array of substrings, 
                                            // length should indicate how many substrings are in the new string
    }



}
公共类问题{
移除的公共字符串重复(字符串s){
字符串newStr=“”;
如果(s.length()==0){
返回s;
}
int length=s.length()-1;
对于(inti=0;i而言,“预期”和“实际”是反向的

assertEquals的第一个参数是预期的,第二个是实际的

您看到的错误显然是在这一行触发的:
assertEquals(check.getValue(),459133821L);


你需要交换你的预期值和实际值,然后修正你的计算。如果你想得到459133821L,你仍然会得到错误的答案。我还没有看过你所有的代码,但是这些测试向你展示了输入输出以及什么给了你正确的答案。找出你为什么试图在
te中达到459133821LstRemoveDuplicates
(乍一看,这似乎是随机的,所以我不确定你怎么知道会发生什么),你会解决它。

你能展示你失败的实际代码吗?我的评论是OT,但我倾向于告诉测试的作者在断言上编写适当的JavaDoc和字符串。在这种情况下,JavaDoc将是解释随机数如何在测试中发挥作用的好地方。断言的所有实现(包括内置关键字)支持在出现故障时打印的描述字符串。另外,捕获单元测试中的任何错误是一个非常糟糕的主意。他们应该抛出异常,而不是捕获异常。添加了失败的代码。