简体   繁体   English

如何在Java中测试此void方法?

[英]How to test this void method in java?

This is the class I'm testing: 这是我正在测试的课程:

public class MovieListing implements Listing {

    private BitSet keyVectors = new BitSet();
    private int year;
    private String title;
    private Set<String> keywords;

    public MovieListing(String title, int year) throws ListingException {
        if (title == null || title.isEmpty() || year <= 0) {
              throw new ListingException("Missing Title or year <= 0");
        }
        this.title = title;
        this.year = year;
        keywords = new TreeSet<String>();
    }

    public void addKeyword(String kw) throws ListingException {
        if (kw == null || kw.isEmpty()) {
            throw new ListingException("Missing keywords");
        }
        this.keywords.add(kw);
    }

This is the test for addKeyword method: 这是对addKeyword方法的测试:

@Before
public void setup() throws Exception {
    movieList = new MovieListing(null, 0);
}

@Test
public void addKeywords() throws Exception {
    assertEquals(0, movieList.getKeywords().size());
    movieList.addKeyword("test1");
    assertEquals(1, movieList.getKeywords().size());        
    movieList.addKeyword("test2");
    assertEquals(2, movieList.getKeywords().size());
}

Where goes wrong? 哪里出问题了? It can't pass. 它无法通过。 Thank you for any advice! 感谢您的任何建议!

And how can I test the Exception in the class cause it doesn't work if I use @Test(expected=Exception.class) 以及如何在类中测试异常 ,原因是如果我使用@Test(expected=Exception.class)则该异常不起作用

You're initializing title with null here: 您要在此处初始化null title

@Before
public void setup() throws Exception {
    movieList = new MovieListing(null, 0);
}

And then you're throwing an exception at the constructor if it's null : 然后,如果它为null则会在构造函数上引发异常:

if (title == null || title.isEmpty() || year <= 0) {
      throw new ListingException("Missing Title or year <= 0");
}

Notwithstanding the fact that you're going to invoke error conditions if you pass null in, or if the year is 0 (thanks to this wonderful condition, which may be entirely valid): 尽管如果传入nullyear为0,您将要调用错误条件,这要归功于:(由于这种奇妙的条件,这可能是完全有效的):

if (title == null || title.isEmpty() || year <= 0) {
    throw new ListingException("Missing Title or year <= 0");
}

...you're missing the exposure of the keywords set in some way or capacity. ...您错过了以某种方式或能力设置的keywords的暴露范围。

Since your method is void , you can't assert against what you're going to get back from the method. 由于您的方法是void ,因此您无法断言要从该方法获得的收益。 So, you have to check the internal state of the entity that you're dealing with. 因此,您必须检查要处理的实体的内部状态。

This is easily accomplished with a package-private getter on the keywords field: 这可以通过在keywords字段上使用package-private getter轻松完成:

Set<String> getKeywords() {
    return keywords;
}

At that point, be sure that your test class is in the same package as your actual code. 此时,请确保您的测试类与实际代码位于同一程序包中。

Further, I would advise against setting up that sort of initial data in init . 此外,我建议不要在init中设置这种初始数据。 I'd consider each individual test a blank slate which needs to initialize the entity that we want to test. 我认为每个测试都是空白,需要初始化我们要测试的实体。 It'd be a simple matter of moving the instantiation to within the test itself. 将实例移动到测试本身内部很简单。

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

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