简体   繁体   English

如何在返回void的方法上运行junit测试?

[英]How to run junit tests on a method that returns void?

I cannot change the signature of the method that needs to be tested . 我无法更改需要测试的方法的签名 The test code looks like below 测试代码如下所示

Parser test = new Parser(props);
ArrayList<HDocument> list = new ArrayList<HDocument>();

test.parse("/users/mac/test.xml", list);
System.out.println("Size of list: "+list.size());
assertEquals(5, list.size());

parse method signature is as below parse方法签名如下

public void parse(String filename, Collection<HDocument> docs)

The parse method runs fine, but when I run the tester, the list size is always 0. I cannot change the parse method signature. 解析方法运行良好,但是当我运行测试仪时,列表大小始终为0。我无法更改解析方法签名。 How should I do it? 我该怎么办?

Here is the Parser class, 这是解析器类,

class Parser{
private Collection<HDocument> docs;
     public void parse(String filename, Collection<HDocument> docs) {
        docs = new ArrayList<HDocument>();
        Collection<HDocument> docsFromXml = new ArrayList<HDocument>();

            Handler hl = new Handler();
            try {
                docsFromXml = hl.getDocs(filename);
            } catch (Exception e) {
                e.printStackTrace();
            }
            docs = docsFromXml;
            System.out.println("Size:"
                    + docs.size()); // This prints the size correctly

        }
    }

}

If parse is supposed to add the results into the docs collection, and the size of docs is zero after you run the parse method, then your test is telling you that parse is broken, or you're calling it wrong. 如果应该使用parse将结果添加到docs集合中,并且在运行parse方法之后docs的大小为零,则您的测试将告诉您该parse已损坏,或者您将其称为错误。 That's what tests are supposed to do: tell you that something isn't working. 那就是测试应该做的:告诉您某些事情不起作用。

In short: you're testing parse correctly, and your test is correctly telling you something else is broken. 简而言之:您正在测试parse正确,并且您的测试正确地告诉您其他错误。 Your test is fine; 您的测试还不错; it's parse that must be wrong somehow. parse肯定某种程度上是错误的。 (Perhaps the question you should be asking StackOverflow is how to fix your parse method.) (也许您应该问StackOverflow的问题是如何解决您的parse方法。)

The error is the parse-method itself. 错误是解析方法本身。

public void parse(String filename, Collection<HDocument> docs) {
    docs = new ArrayList<HDocument>(); /* First problem here: The method should add values to the parameter not to a new List-Instance */
    [...]
    docs = docsFromXml; // second error here. you overwrite the list again.

Should be something like: 应该是这样的:

public void parse(String filename, Collection<HDocument> docs) {
        if(docs==null) throw new IllegalArgumentException("no list for adding values specified");
        if(filename==null) throw new IllegalArgumentException("no filename specified");
        Handler hl = new Handler();
        try {
            docs.addAll(hl.getDocs(filename));
        } catch (Exception e) {
            throw new RuntimeEception(e); // never sink exception without proper handling
        }

    }
}

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

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