开发者

how to write test case in java

hi I created one class ABC.java, and there is one constructor ABC() as follows:

 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.newDocumentB开发者_高级运维uilder();
                  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


Although Selenium is a test tool itself, it sounds to me as if you want to test this class itself. Like implementing a test with 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜