Junit测试构造函数中的异常?

时间:2014-03-17 22:11:03

标签: java exception junit

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

// 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;
    }

}

3 个答案:

答案 0 :(得分:4)

您的构造函数不会执行您的setter检查的任何检查,因此它不会抛出任何异常。直接分配实例变量时,不会调用setter;他们只是明确地打电话。

让构造函数调用您的setter,或者在构造函数中实现验证。

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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

相关问题