简体   繁体   English

关于junit测试用例

[英]Regarding junit test cases

I am new to the world of Junit test cases , I just wanted to know let say if I developed a program 我是Junit测试用例领域的新手,我只是想知道是否说我开发了一个程序

class MapDemo1
{static final Logger logger = Logger.getLogger(MapDemo1.class);
     /**
     *  
     */
    public static void main(String arg[])
     {PropertyConfigurator.configure("src/log4j.properties");
     logger.info("-->Map");
    // Map map=new TreeMap();

         Map map=new HashMap();//HashMap key random order.
      //   System.out.println("Amit".hashCode());
         map.put("Amit","Java");
         map.put("Amit","Javas");
        // map.put("mAit","J2EE");
         //map.put("Saral","J2rrrEE");
         /*map.put("ty","Spring");
         map.put("Anupam","Hibernate");
         map.put("Ravi",".Net");
         map.put("Saral","Andriod");//same key but different value 
         map.put("Nitin","PHP");
         map.put("hj","Spring1");*/
         System.out.println("There are "+map.size()+" elements in the map.");
         System.out.println("Content of Map are...");
         Set s=map.entrySet();
         Iterator itr=s.iterator();
         while(itr.hasNext())
         {
             Map.Entry m=(Map.Entry)itr.next();
             System.out.println(m.getKey()+"\t"+m.getValue()+"\t"+ m.hashCode());
          }
]
}

Now please advise what are the junit test cases for this and how they will be written in seprate class. 现在,请告知用于此目的的junit测试用例以及如何在单独的类中编写它们。

As is, this program is not testable using JUnit -- it's too small, and it's all in one method. 照原样,该程序无法使用JUnit进行测试-它太小了,而且全部使用一种方法。 To make it testable, you need to break it apart into parts that do just one thing. 为了使其可测试,您需要将其分解为仅可做一件事情的部分。

One such part could be populateMap() , a method that created the HashMap and added the values. 其中一部分可以是populateMap() ,这是一种创建HashMap并添加值的方法。 Then you could add one or two tests that called that method and tested the size and content of the map. 然后,您可以添加一个或两个调用该方法的测试,并测试地图的大小和内容。

Printed output is also hard (if not impossible) to test. 打印输出也很难(如果不是不可能的话)进行测试。 One way to deal with this would be to put the code that printed output into a method that accepted a PrintWriter as a parameter. 一种解决方法是将打印输出的代码放入接受PrintWriter作为参数的方法中。 In the real program, call it and pass it something connected to System.out . 在实际程序中,调用它并传递一些与System.out连接的东西。 In a test, you could call it with a PrintWriter connected to a StringWriter , and then examine the contents of that StringWriter . 在测试中,可以使用连接到StringWriterPrintWriter调用,然后检查该StringWriter的内容。

In general, you don't test programs with JUnit; 通常,您不使用JUnit测试程序 you test units . 您测试单位 A unit is either a class, or a small group of tightly-coupled classes, that provide some kind of well-defined service to the rest of a program. 一个单元可以是一个类,也可以是一小组紧密耦合的类,它们为程序的其余部分提供某种定义明确的服务。 For example, a class like StringBuilder is a fairly large and complex unit, and hopefully you can imagine dozens or hundreds of tests you might write for it. 例如,诸如StringBuilder类的类是一个相当大而复杂的单元,希望您可以想象您可能会为此编写数十或数百个测试。

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

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