Java:单元测试没有返回正确的结果

时间:2016-11-06 21:03:26

标签: java unit-testing

我正在编写一个程序,如果没有对游戏进行任何评论,那么我需要将其添加到地图中。如果有游戏评论,则将给定评论添加到相应的GameInfo。我的代码编译得很好,但是我的单元测试没有返回我需要的点来返回以显示我的代码正在正确执行。这是我的代码:

class GameInfoCollection {
    // TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's
    Map<String, Integer> titles = new HashMap<String, Integer>();
    // TODO - if there are no reviews for the game, create a new GameInfo  (with this review) and add it to the map
    // if there's one, add the given review to the corresponding GameInfo 
    public void addGameReview(String gameTitle, Review r)
    {
        if (titles.isEmpty()) {
            GameInfo g = new GameInfo("Review");
        } else titles.put(gameTitle, 1);

    }

    public int getNumberOfReviewsForGame(String gameTitle)
    {
        // TODO - implement this
        return titles.get(titles);
    }

这是我对这些方法的单元测试。它应该返回20分,但它不会返回任何点:

public void testGetNumberOfReviewsForGame()
    {       
        GameInfoCollection gic=new GameInfoCollection();


        gic.addGameReview("g1",new Review("cool",5));
        gic.addGameReview("g1",new Review("cool",3));
        gic.addGameReview("g2",new Review("cool",2));
        gic.addGameReview("g3",new Review("cool",2));

        Assert.assertEquals(2,gic.getNumberOfReviewsForGame("g1"));
        Assert.assertEquals(1,gic.getNumberOfReviewsForGame("g2"));
        gic.addGameReview("g1",new Review("cool",3));
        Assert.assertEquals(3,gic.getNumberOfReviewsForGame("g1"));
    }

    @Grade(points=20)
    @Test

以下是我的整个程序的代码。请注意,最后还有部分内容尚未完成。

package assignment;

import java.text.MessageFormat;
import java.util.Scanner;

import java.awt.Point;
import java.awt.Dimension;
import java.awt.Rectangle;

import java.time.LocalDate;

import java.util.*; // List, ArrayList, Map, HashMap

class Review {
    public String reviewText;
    public int numberOfStars;

    public Review(String reviewText, int numberOfStars) {
        this.reviewText=reviewText;
        this.numberOfStars=numberOfStars;
    }
}

class GameInfo {
    private String title;
    // need an ArrayList to keep the reviews;
    private Review[] reviews = new Review[10];
    int numReviews=0;

    public GameInfo(String title) {
        this.title=title;
        // you may want to initialize any other variables you create here
    }

    public String getTitle() {
        return title;
    }

    // TODO - adds the review to the 'array' of reviews. You need to keep all reviews in an array
    public void addReview(Review r) {
        reviews[numReviews] = r;
        ++numReviews;
    }

    // TODO - returns the number of reviews which have been added to this GameInfo
    public int getNumberOfReviews() {
        return numReviews;
    }

    // TODO - returns the sum of the number of stars which have been added to this GameInfo
    // you have to calculate this from your array
    public int getSumOfStars() {
        int sum=0;
        for (int i=0; i<numReviews;++i)
            sum +=reviews[i].numberOfStars;
        return sum;
    }

    // TODO - returns the average number of stars for this GameInfo's reviews
    // again, have to calculate this (or at least the sum of stars) from your array
    public double getAverageStarRating() {
        double firstNumber = getSumOfStars();
        double secondNumber = getNumberOfReviews();
        double avg = firstNumber/secondNumber;

        return avg;
    }
}

// TODO - you need to implement all these methods
class GameInfoCollection {
    // TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's
    Map<String, Integer> titles = new HashMap<String, Integer>();
    // TODO - if there are no reviews for the game, create a new GameInfo  (with this review) and add it to the map
    // if there's one, add the given review to the corresponding GameInfo 
    public void addGameReview(String gameTitle, Review r)
    {
        if (titles.isEmpty()) {
            GameInfo g = new GameInfo("Review");
        } else titles.put(gameTitle, 1);

    }

    public int getNumberOfReviewsForGame(String gameTitle)
    {
        // TODO - implement this
        return titles.get(titles);
    }

请注意,我还是Java的新手,感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

这看起来像是家庭作业,我猜测单元测试是老师提供的代码的一部分。单元测试失败的事实意味着,不是单元测试错误,而是您的代码行为不正确。特别是GameInfoCollection中您尚未完成的部分。

所以,让我们回顾一下每个TODO评论意味着什么:

// TODO - you need to use a Map (from a String, the title, to a GameInfo) to keep track of all the GameInfo's

地图有键和值,据说从键映射到值。在声明Map<String, Integer>中,第一个类型(String)是键的类型,第二个是值的类型。这里的TODO表示值的类型必须是GameInfo

// TODO - if there are no reviews for the game, create a new GameInfo  (with this review) and add it to the map

这一部分的每一部分都需要在if分支内完成。您需要a)创建一个新的GameInfo,b)将评论放入其中,然后c)将其添加到地图中。您目前只执行a)部分,即使您没有将正确的值传递给构造函数 - 请查看GameInfo构造函数及其所需参数的名称; &#34;评论&#34;匹配那个?

// if there's one, add the given review to the corresponding GameInfo 

这是关于else分支的内容。

// TODO - implement this

从技术上讲,此方法已经有一个实现,但您需要更改它以正确匹配第一个TODO关于地图值类型的更改。