返回空指针异常但是期望MovieListException?

时间:2018-04-23 04:11:31

标签: java junit exception-handling nullpointerexception try-catch

所以我正在运行一个JUnit测试,它获得电影的评级( Hashmap HashMap<String, Rating> movieList) 但是如果有的话对电影投掷MovieListException没有评级。

JUNIT预期与实际 https://imgur.com/a/mdQfJgT

了解抛出异常所以我不知道我做错了什么?

  • 在Rating类中,我检查对象值是否为null,然后抛出MovieListException
  • MovieList类中,此行的错误为空:Integer value = movieList.get(string).getRating();即使我正在捕捉并返回电子对象 (我认为它需要返回?基于JUNIT TEST?)
  

问题:我得到一个空指针而不是我的MovieListException

测试类

    /* Test 5: Can't get a rating for an unrated movie
     */
    @Test(expected = MovieListException.class)
    public void nonexistentRating() throws MovieListException {
        String result = movies.getRating("The Ghost in the Invisible Bikini");

    }

MovieList类

public class MovieList {
private HashMap<String, Rating> movieList = new HashMap<String, Rating>();

public void addMovie(String string) {
    // TODO Auto-generated method stub
    movieList.put(string, new Rating(RatingType.NONE, null));
}



public String getRating(String string) throws MovieListException {
    // TODO Auto-generated method stub
    String ratingString = "";

    try {
        Integer value = movieList.get(string).getRating();

        if(value != null && value > 0) {
            for(int i = 0; i < value; i++) {
                ratingString += "*";
            } 
            return ratingString;
        }                   
    } catch(MovieListException e) {
        System.out.print(e.getMessage());
        return e.getMessage();
    }


    return "No rating";
}

public void setRating(String string, Rating rating) {
    // TODO Auto-generated method stub
    movieList.replace(string,  rating);
}

package movieListQuestion;

评级等级

public class Rating {


private Integer ratingValue;
private RatingType typeValue;

public Rating(RatingType type, Integer rating) {
    this.ratingValue = rating;
    this.typeValue = type;
}   

public Integer getRating() throws MovieListException {
    if(this.ratingValue == null) {
        throw new MovieListException("No rating");
    }
    return this.ratingValue;
}

}

异常类

    package movieListQuestion;

/* A trivial exception class for the Movie List program.
 */
@SuppressWarnings("serial")
public class MovieListException extends Exception {

    public MovieListException() {
        super();
    }

    public MovieListException(String message) {
        super(message);
    }

}

来自MovieList类的更新getRating方法

public String getRating(String string) throws MovieListException {
        // TODO Auto-generated method stub
        String ratingString = "";

            Integer value = movieList.get(string).getRating();

            if (!movieList.containsKey(string)) {
               throw new MovieListException("Movie not found");
            }

            if(value != null && value > 0) {
                for(int i = 0; i < value; i++) {
                    ratingString += "*";
                } 
                return ratingString;
            }                   


        return "No rating";
    }

2 个答案:

答案 0 :(得分:1)

NullPointerException movieList.get(string).getRating()显示您搜索的电影不在movieList

您可能决定在找不到电影时抛出相同的MovieListException。你可以这样做

if (!movieList.containsKey(string)) {
   throw new MovieListException("Movie not found");
}
//Rest of the code

//Or using Optional
Rating rating = Optional.ofNullable(movieList.get(string))
        .orElseThrow(() -> new MovieListException("Movie not found"));
Integer value = rating.getRating();

此外,您不应该在MovieListException方法

中捕获getRating

答案 1 :(得分:0)

movieList.get(string)的{​​{1}}为movieList.get(string).getRating();。这意味着您在null中没有名为string的电影。

我认为你应该调用以下方法

movieList

来自你的junits。