Java Junit在构造函数中测试异常?

Java Junit在构造函数中测试异常?,java,exception,junit,Java,Exception,Junit,在下面的测试中,我尝试测试构造函数在传递非法参数时是否抛出异常。在尝试创建对象时,我在每个实例变量的设置器中添加了验证,当传递无效数据时,它们抛出异常 // test for invalid const @Test(expected = IllegalArgumentException.class) public void testInvalidBookStringStringStringInt() { // create a new book

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

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

}
包装实践;
/**
*类创建一本书
* 
*@作者罗斯·杨
* 
*/
公共课堂用书{
//声明实例变量
/**
*书的ISBN
*/
专用字符串ISBN;
/**
*作者
*/
私有字符串作者;
/**
*书名
*/
私有字符串标题;
/**
*书评
*/
私人内部评级;
/**
*默认构造函数
*/
公共书籍(){
}
/**
*带参数的构造函数
* 
*@param-ISBN
*@param作者
*@param title
*@param评级
*/
公共书籍(字符串ISBN、字符串作者、字符串标题、整数评级){
这是ISBN=ISBN;
this.author=作者;
this.title=标题;
这个。评级=评级;
}
/**
*获取ISBN
* 
*@return ISBN
*/
公共字符串getISBN(){
返回ISBN;
}
/**
*设置ISBN
* 
*@param-iSBN
*/
公共无效集合iSBN(字符串iSBN){
如果((iSBN.length()!=9)| |(iSBN.length()!=12)){
抛出新的IllegalArgumentException(“无效的ISBN长度”);
}否则{
这个。ISBN=ISBN;
}
}
/**
*获得作者
* 
*@返回作者
*/
公共字符串getAuthor(){
返回作者;
}
/**
*集作者
* 
*@param作者
*/
公共void setAuthor(字符串编写器){
if((author.length()<0)| |(author.length()>20)){
抛出新的IllegalArgumentException(“无效作者长度”);
}否则{
this.author=作者;
}
}
/**
*获得头衔
* 
*@返回标题
*/
公共字符串getTitle(){
返回标题;
}
/**
*设置标题
* 
*@param title
*/
公共无效集合标题(字符串标题){
if((title.length()<0)| |(title.length()>20)){
抛出新的IllegalArgumentException(“无效标题长度”);
}否则{
this.title=标题;
}
}
/**
*获得评级
* 
*@回报率
*/
public int getRating(){
回报率;
}
/**
*设定额定值
* 
*@param评级
*/
公共评级(内部评级){

如果((评级>5)|(评级您的构造函数正在设置私有变量,而不使用包含验证的公共setter。

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


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

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

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

请显示
Book
类,包括构造函数和尝试验证输入的所有setter。只需编辑它以包含它,您的测试就在setter中。构造函数根本没有检查。您希望在哪里引发异常?this.ISBN=setISBN(ISBN)如果你想避免构建非法的<代码>书<代码> s,你应该考虑使用Builder模式;参数检查可以在那里完成,你不会凌乱你的代码>书< /Cord>类感谢,所以基本上,如果IM检查验证放弃一个期望,那么在与VA方法相关的测试方法中这样做。lidation是。也就是说,在这种情况下测试setter?我会测试构造函数和setter;它们都应该能够验证它们的输入。是的,但是在这种情况下我该如何测试构造函数呢?为每个setter单独进行JUnit测试,为构造函数单独进行JUnit测试——这里总共有5个测试。谢谢,但是我没有测试构造函数中的无效参数?对不起,我不知道你的意思?