使用 main 方法在 Java 中编写单元测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19239158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:20:50  来源:igfitidea点击:

Writing a Unit test in Java with main method

javaunit-testingjunit

提问by Mike Smith

I have a few methods in my class

我的课上有一些方法

public class StringChecking
{
public static void main(String[] args);
public void stringChecker(String text, int number); //takes a string and prints that out. 
}

I want to write Unit test to test the 'stringChecker()' method. I was wondering how I would go about doing that. When I create an object of type StringCheckingin my JUnit Testing class, I can't seem to access stringChecker()method from that instance.

我想编写单元测试来测试“ stringChecker()”方法。我想知道我将如何去做。当我StringChecking在 JUnit 测试类中创建一个类型的对象时,我似乎无法stringChecker()从该实例访问方法。

StringChecker method prints out certain number of words of the text passed in depending on the parameter.I wish to check to see if the first 10 words getting printed out are same as the expected results.

StringChecker 方法根据参数打印出传入文本的特定数量的单词。我想检查打印出来的前 10 个单词是否与预期结果相同。

JUnit test class

JUnit 测试类

String expected = "My name is";
asserEquals(expected, actual);

I'm guessing I will have make my stringChecker method return something, inorder to do the check. But I don't understand why I can't access the method from testing class.

我猜我会让我的 stringChecker 方法返回一些东西,以便进行检查。但我不明白为什么我无法从测试类访问该方法。

采纳答案by Makoto

If you're attempting to assert values coming back, then your method signature is incorrect - it should be returning a String. From there, you can perform the usual operation with JUnit (your mainmethod serves no purpose in this context):

如果您尝试断言返回的值,那么您的方法签名不正确 - 它应该返回一个String. 从那里,您可以使用 JUnit 执行通常的操作(您的main方法在这种情况下没有用处):

@Test
public void checkString() {
    String expected = "My name is";
    int actual = 3; // assuming words in string
    StringChecking testObj = new StringChecking();
    assertEquals(expected, testObj.stringChecker(expected + " Bob", 3);
}

Merely printing out values asserts/validates nothing - they provide visual confirmation, which can be often incorrect or invalid.

仅仅打印出值不会断言/验证任何东西——它们提供视觉确认,这通常可能是不正确或无效的。

Java has to know what those values are, and the most direct way to do that is to return the value you need back.

Java 必须知道这些值是什么,最直接的方法是返回您需要的值。

This isn't to say that you can nevertest a voidmethod, but that in this case, it's not appropriate.

这并不是说你永远不能测试一个void方法,但在这种情况下,它是不合适的。

回答by Thilo

The code would be easier to test if instead of printing the numbers, the method returned them to the caller.

如果该方法不打印数字,而是将它们返回给调用者,则代码会更容易测试。

If you want to test the actual printing, you could set a spy object to System.outthat instead of printing collects the data. Then your test can assert the correctness of that output.

如果您想测试实际打印,您可以将一个间谍对象设置为 System.out,而不是打印收集数据。然后您的测试可以断言该输出的正确性。