嗨,我在尝试使用无效参数 JUNIT 测试我的构造函数时遇到问题。我的测试中的其他所有内容都有效,但是当我尝试使用无效的构造函数值执行测试用例时,出现以下错误:
“预期会抛出 IllegalArgumentException 但没有抛出任何内容”。
这是我的代码。
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");
}
}
}
以下是我的测试用例代码。
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());
}
}
** 艺术家代码
打包歌曲;
公开课艺术家扩展歌曲{
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");
}
}
答案 0 :(得分:0)
您已在 setArtistName
方法中正确实施了验证。
/**
* @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");
}
}
但是您的构造函数不使用此方法,因此它会跳过验证。
public Artist(String name, String genre, double length, int chartNumber, String artistName) {
super(name, genre, length, chartNumber);
this.artistName = artistName;
}
您需要改为调用 setter 方法。
public Artist(String name, String genre, double length, int chartNumber, String artistName) {
super(name, genre, length, chartNumber);
this.setArtistName(artistName); // or just setArtistName(artistName);
}