JUnit testing failing with constructors

Hi there I am having trouble trying to JUNIT test my Constructors with invalid args. Everything else in my test works but when I try to execute the test case with the invalid constructors values I am getting following error:

"Expected IllegalArgumentException to be thrown but nothing was thrown".

here is my code.

package song;
public abstract class song {

    private String name;
    private String genre;
    private double length;
    private int chartNumber;
    
    /**
     * Setting the limit values 
     * 
     */
    public final static int NAME_LOWER_LIMIT = 1;
    public final static int CHART_LOWER_LIMIT = 1;
    public final static int CHART_UPPER_LIMIT = 10;
    public final static int GENRE_LENGTH = 1;
    public final static double LENGTH_LOWER_LIMIT = 1.00;
    public final static double LENGTH_UPPER_LIMIT = 5.00;
    

    public song() {
    
    }
    
    

    public song(String name, String genre, double length, int chartNumber) {
        super();
        this.name = name;
        this.genre = genre;
        this.length = length;
        this.chartNumber = chartNumber;
    }



    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     * Name must have at least one character
     */
    public void setName(String name) throws IllegalArgumentException{
        if(name.length()>=NAME_LOWER_LIMIT) {
        this.name = name;
        } else {
            throw new IllegalArgumentException("Name value too short");
        }
    }

    /**
     * @return the genre
     */
    public String getGenre() {
        return genre;
    }

    /**
     * @param genre the genre to set
     * Genre must be at least one charter
     */
    public void setGenre(String genre) throws IllegalArgumentException {
        if(genre.length()>=GENRE_LENGTH) {
            this.genre = genre;
        } else {
            throw new IllegalArgumentException("Genre name too short");
        }
    }

    /**
     * @return the length
     */
    public double getLength() {
        return length;
    }

    /**
     * @param length the length to set
     * Length has to be greater than 1.00 and less than 5.00
     */
    public void setLength(double length) throws IllegalArgumentException {
        if((length >= LENGTH_LOWER_LIMIT) && (length <= LENGTH_UPPER_LIMIT)) {
            this.length = length;
        } else {
            throw new IllegalArgumentException("song length not inside the boundries");
        }
    }

    /**
     * @return the chartNumber
     * Chart limit has to be between 1 -20
     */
    public int getChartNumber() {
        return chartNumber;
    }

    /**
     * @param chartNumber the chartNumber to set
     * Chart number must be between 1 -10
     */
    public void setChartNumber(int chartNumber) throws IllegalArgumentException {
        if((chartNumber >= CHART_LOWER_LIMIT) && (chartNumber<= CHART_UPPER_LIMIT)) {
            this.chartNumber = chartNumber;
        } else {
            throw new IllegalArgumentException("Chart not in bounds");
        }
    }

}

Following is the my test case code.

package testartist;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import song.Artist;

class TestArtist {
    
    String name,nameInvalid, genre,genreInvalid, artistName, artistNameInvalid;
    int chartNumber, chartNumberInvalid;
    double length, lengthInvalid;
    

    @BeforeEach
    void setUp() throws Exception {
        
        name = "Beautiful day";
        nameInvalid = "";
        genre = "Country";
        genreInvalid = "";
        artistName = "Bono";
        artistNameInvalid = "";
        chartNumber = 3;
        chartNumberInvalid = 15;
        length = 2.56;
        lengthInvalid = 5.45;
    }

    @Test
    void testDefaultConstructor() {
        
        Artist song = new Artist();
        
        
    }

    @Test
    void testConstructorWithArgs() {
        
        Artist artist = new Artist(name, genre, length, chartNumber, artistName);
        
        assertEquals(name, artist.getName() );
        assertEquals(genre, artist.getGenre() );
        assertEquals(length, artist.getLength() );
        assertEquals(chartNumber, artist.getChartNumber());
        assertEquals(artistName, artist.getArtistName());
    }
    
    @Test
    void testInvalidConstructorWithArgs() {
        
        assertThrows(IllegalArgumentException.class, () -> {
            Artist artist = new Artist(name, genre, length, chartNumber, artistNameInvalid);
            }); 
        
    }

    
    

@Test
    void testSetGetArtistName() {
    
    Artist a = new Artist();
    a.setArtistName(artistName);
    assertEquals(artistName, a.getArtistName());
    }

    @Test
    void testSetGetName() {
    Artist a = new Artist();
    a.setName(name);
    assertEquals(name, a.getName());
    }

    @Test
    void testSetGetGenre() {
        Artist a = new Artist();
        a.setGenre(genre);
        assertEquals(genre, a.getGenre());
    }

    @Test
    void testSetGetLength() {
        Artist a = new Artist();
        a.setLength(length);
        assertEquals(length, a.getLength());
        
    }

    @Test
    void testSetGetChartNumber() {
        Artist a = new Artist();
        a.setChartNumber(chartNumber);
        assertEquals(chartNumber, a.getChartNumber());
    }

}

** Artist code

package song;

public class Artist extends song {

private static final int ARTIST_NAME_LOWER_LIMIT = 1;

private String artistName;

public Artist() {
    
}

public Artist(String name, String genre, double length, int chartNumber, String artistName) {
    super(name, genre, length, chartNumber);
    this.artistName = artistName;
}

/**
 * @return the artistName
 */
public String getArtistName() {
    return artistName;
}

/**
 * @param artistName the artistName to set
 */
public void setArtistName(String artistName) throws IllegalArgumentException{
    if( artistName.length()>= ARTIST_NAME_LOWER_LIMIT) {
        this.artistName = artistName;
    } else {
        throw new IllegalArgumentException("Name too short");
    }
}

37 thoughts on “JUnit testing failing with constructors”

  1. 226592 43844When do you feel this Real Estate market will go back in a positive direction? Or is it nonetheless too early to tell? We are seeing plenty of housing foreclosures in Altamonte Springs Florida. What about you? Would really like to get your feedback on this. 486131

    Reply

Leave a Comment