简体   繁体   English

Java的新手-返回单元测试失败

[英]New to Java — Return Unit test failing

Im working on a Java program in class and im unsure why or how this is failing considering I get the correct print out when the program is ran. 我正在课堂上研究Java程序,不确定运行该程序的原因或原因,因为我在运行该程序时会得到正确的打印结果。

Here is the code I have: 这是我的代码:

import java.util.Scanner;

public class TextAnalyzer {

    // Create scanner

    private static Scanner scnr = new Scanner(System.in);
    // Create text string
    private static String text;

    public static void main(String[] args) {
        System.out.println("Enter a sentence or phrase: ");
        // Scan for text input
        text = scnr.nextLine();

        System.out.println("You entered: " + text);

        // Call getNumOfCharacters
        int count = getNumOfCharacters();
        System.out.println();
        System.out.println("Number of characters: " + count);
        // Call outputWithoutWhitespace
        String modifiedstring = outputWithoutWhitespace();
        System.out.println("String with no whitespace: " + modifiedstring);

    }

    // Method outputs string without spaces

    private static String outputWithoutWhitespace() {
        text = text.trim().replaceAll(" ", "");
        return text;
    }

    // Method to return number of characters

    private static int getNumOfCharacters() {
        return text.length();
    }
}

The output passes on all levels, it's the Unit test for the number of characters in the input that is failing and I really just need some guidance as a new student to Java programming. 输出通过所有级别,它是输入中字符数失败的单元测试,作为Java编程的新学员,我真的只需要一些指导。

Here is a printout of the tests: 这是测试的打印输出:

在此处输入图片说明

My assumption is that the getNumOfCharacters() is returning the number of characters AFTER the whitespaces have been removed, but I could be wrong. 我的假设是,在删除空格后, getNumOfCharacters()返回的字符数是多少,但我可能是错的。

Any help/guidance would be greatly appreciated. 任何帮助/指导将不胜感激。

Your assumption is correct and the problem is that you are replacing text with the stripped text : 你的假设是正确的,问题是要替换text剥离 text

private static String outputWithoutWhitespace() {
    text = text.trim().replaceAll(" ", "");
    return text;
}

... and now getNumOfCharacters() is returning the stripped length, which is no longer eg 46. ...,现在getNumOfCharacters()返回的剥离长度不再是例如46。

That is, when you hit this line in main : 也就是说,当您在main点击此行时:

String modifiedstring = outputWithoutWhitespace();

It has the side-effect of replacing text , since that's precisely what you told outputWithoutWhitespace() to do. 它具有替换text的副作用,因为这正是您告诉outputWithoutWhitespace()要做的。 So by the time main ends, text now contains the modified text, and subsequent calls to getNumOfCharacters() fail the unit tests. 因此,到main结束时, text现在包含已修改的文本,并且随后对getNumOfCharacters()调用getNumOfCharacters()通过单元测试。

Your printed output is misleading (still prints "46") because you compute and store the character count before you mess up text in outputWithoutWhitespace() . 您的打印输出具有误导性(仍打印“ 46”),因为text弄乱到outputWithoutWhitespace() 之前,您已经计算并存储了字符数。

This is a good exercise in how to process and manage data, and a good learning experience for you. 这是有关如何处理和管理数据的良好练习,并且对您来说是一种良好的学习体验。 It's also a nice demonstration of the value of unit tests in quality control; 这也很好地证明了单元测试在质量控制中的价值。 you should remember this lesson. 您应该记住这一课。

Here's a hint: Based on your application requirements, does outputWithoutWhitespace() really need to store the trimmed text? 提示:根据您的应用程序要求, outputWithoutWhitespace() 真的需要存储修剪后的文本? Or does it just need to return it? 还是只需要退货?

In the future, for debugging, consider: 将来,为了进行调试,请考虑:

  • Stepping through in a debugger where possible to examine what's happening along the way. 在可能的情况下逐步进入调试器,以检查过程中发生的情况。
  • Adding diagnostic printouts, for example, print the value of text in getNumOfCharacters() to verify that it is what you think it is. 例如,添加诊断打印输出,在getNumOfCharacters()打印text的值,以验证它是否与您想的一样。 Especially given your assumption that the problem was here and the failed unit test, this would be a good place to start investigating. 尤其是假设您认为问题出在这里并且单元测试失败,那么这将是开始调查的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM