user3287264
user3287264

Reputation:

Junit testing for exceptions in a constructor?

In the following test I am trying to test if an exception has been thrown by the constructor when it is passed illegal arguments when trying to create the object, I have added validation in the setters of each instance variable that when they are passed invalid data they throw an exception.

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

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

    }

The test is currently failing at the minute, what am i doing wrong?

Book class:

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

}

Upvotes: 4

Views: 4679

Answers (3)

fastcodejava
fastcodejava

Reputation: 41077

You need to call the setX methods from your constructor. Also change setISBN to :

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

Upvotes: 2

rgettman
rgettman

Reputation: 178243

Your constructor doesn't perform any of the checks that your setters check, so it won't ever throw any exceptions. Setters aren't called when you directly assign an instance variable; they're only called explicitly.

Either have your constructor call your setters, or implement the validation in your constructor.

Upvotes: 4

CoderDennis
CoderDennis

Reputation: 13837

Your constructor is setting the private variables and not using the public setters that contain the validation.

Upvotes: 1

Related Questions