简体   繁体   English

如何在Java中编写测试用例

[英]how to write test case in java

hi I created one class ABC.java, and there is one constructor ABC() as follows: 嗨,我创建了一个类ABC.java,并且有一个构造函数ABC()如下:

 public class ABC {

        private static String host;
        private static String port;
        ------ 

       public ABC(){
            try { 

                  File file = new File("Element.xml");
                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  Document doc = db.parse(file);                    

I am passing Element.xml as a input,I want to test this class,so I created ABCTest class, please tell me how to write test case for constructor ABC() and how to write assert(),for above code 我正在传递Element.xml作为输入,我想测试该类,所以我创建了ABCTest类,对于以上代码,请告诉我如何为构造函数ABC()编写测试用例以及如何编写assert()

Although Selenium is a test tool itself, it sounds to me as if you want to test this class itself. 尽管Selenium本身就是一个测试工具,但对我来说听起来好像您想测试此类本身。 Like implementing a test with jUNit. 就像用jUNit进行测试一样。

With the snippet you just provided, there's not much to test, because you don't change the objects state (yet). 使用您刚刚提供的代码片段,没有什么要测试的,因为您还没有更改对象的状态。 The instance variables are not initialized (yet). 实例变量尚未初始化。 The constructor does something but does not produce a result. 构造函数会执行某些操作,但不会产生结果。

You could write a pretty small test case to check if there's not Exception being thrown during initialization. 您可以编写一个很小的测试用例,以检查初始化期间是否没有引发异常。 Although this may be difficult, because you catch exceptions in the constructor. 尽管这可能很困难,但是因为您在构造函数中捕获了异常。

And you may consider redirecting System.out to a string during the test and check if the code prints the expected output to the console. 并且您可以考虑在测试期间将System.out重定向到字符串,并检查代码是否将预期的输出打印到控制台。

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

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