【问题标题】:Junit testing for exceptions in a constructor?Junit测试构造函数中的异常?
【发布时间】:2014-03-17 22:11:02
【问题描述】:

在下面的测试中,我试图测试构造函数在尝试创建对象时传递非法参数时是否抛出异常,我在每个实例变量的设置器中添加了验证,当它们被传递时无效数据他们抛出异常。

// test for invalid const
    @Test(expected = IllegalArgumentException.class)
    public void testInvalidBookStringStringStringInt() {

        // create a new book
        Book b = new Book(invalidISBN, invalidAuthor, invalidTitle,
                invalidRating1);

    }

测试目前正在失败,我做错了什么?

书籍类:

package practice;

/**
 * Class that creates a Book
 * 
 * @author Ross Young
 * 
 */
public class Book {

    // declaring instance variables
    /**
     * Book's ISBN
     */
    private String ISBN;

    /**
     * Book's Author
     */
    private String author;

    /**
     * Book's Title
     */
    private String title;

    /**
     * Book's rating
     */
    private int rating;

    /**
     * Default constructor
     */
    public Book() {

    }

    /**
     * Constructor with Args
     * 
     * @param ISBN
     * @param author
     * @param title
     * @param rating
     */
    public Book(String ISBN, String author, String title, int rating) {
        this.ISBN = ISBN;
        this.author = author;
        this.title = title;
        this.rating = rating;

    }

    /**
     * Gets ISBN
     * 
     * @return ISBN
     */
    public String getISBN() {
        return ISBN;
    }

    /**
     * Sets ISBN
     * 
     * @param iSBN
     */
    public void setISBN(String iSBN) {

        if((iSBN.length()!=9)||(iSBN.length()!=12)){

            throw new IllegalArgumentException("Invalid ISBN length");

        }else{

                this.ISBN=iSBN;
            }

        }


    /**
     * Gets Author
     * 
     * @return author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * Sets author
     * 
     * @param author
     */
    public void setAuthor(String author) {

        if ((author.length() < 0) || (author.length() > 20)) {

            throw new IllegalArgumentException("Invalid author Length");
        } else {
            this.author = author;

        }
    }

    /**
     * gets title
     * 
     * @return title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Sets title
     * 
     * @param title
     */
    public void setTitle(String title) {

        if ((title.length() < 0) || (title.length() > 20)) {

            throw new IllegalArgumentException("Invalid title Length");

        } else {

            this.title = title;

        }

    }

    /**
     * Gets rating
     * 
     * @return rating
     */
    public int getRating() {
        return rating;
    }

    /**
     * Sets rating
     * 
     * @param rating
     */
    public void setRating(int rating) {

        if((rating>5)|| (rating<1)){
            throw new IllegalArgumentException("Rating invalid");
        }
        this.rating = rating;
    }

}

【问题讨论】:

  • 请显示Book 类,包括构造函数和所有尝试验证输入的设置器。
  • 只是编辑它以包含它,干杯
  • 您的测试在 setter 中。构造函数根本没有检查。您希望在哪里抛出异常?
  • this.ISBN = setISBN(ISBN) in constructor???
  • 如果要避免构建非法Books,应该考虑使用builder模式;参数检查可以在那里完成,你不会弄乱你的 Book

标签: java exception junit


【解决方案1】:

您的构造函数不会执行您的 setter 检查的任何检查,因此它永远不会抛出任何异常。直接分配实例变量时不会调用 Setter;它们只是被显式调用。

要么让你的构造函数调用你的setter,要么在你的构造函数中实现验证。

【讨论】:

  • 谢谢,所以基本上如果我检查验证抛出一个期望,在与验证方法相关的测试方法中执行此操作。即在这种情况下测试设置器?
  • 我会同时测试构造函数和设置器;他们都应该能够验证他们的输入。
  • 是的,但是在这种情况下我将如何测试构造函数?
  • 对每个 setter 进行单独的 JUnit 测试,并为构造函数进行单独的 JUnit 测试——这里总共有 5 个测试。
  • 谢谢,但我没有在构造函数中测试无效参数?
【解决方案2】:

您需要从构造函数中调用setX 方法。还将setISBN 更改为:

if((iSBN.length()!=9) && (iSBN.length()!=12)) {
   throw new IllegalArgumentException("Invalid ISBN length");

【讨论】:

    【解决方案3】:

    您的构造函数正在设置私有变量,而不是使用包含验证的公共设置器。

    【讨论】:

    • 对不起,我不确定你的意思?
    猜你喜欢
    • 2013-09-28
    • 2014-05-27
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    相关资源
    最近更新 更多