当前位置: 首页 > 知识库问答 >
问题:

JUnit测试java方法,测试没有返回类型的方法是正常的,还是应该对它们使用不同类型的测试?

南门承教
2023-03-14

谢谢

  public GamePlay(int decade)
    {
        this();
        questions = null;
        if(decade== 1960)
        {
            questions = Questions.QuestionSetFrom60s();


        }
        else if(decade== 1970)
        {
            questions = Questions.QuestionSetFrom70s();

        }
        else if(decade== 1980)
        {
            questions = Questions.QuestionSetFrom80s();

        }
        else if(decade== 1990)
        {
            questions = Questions.QuestionSetFrom90s();

        }
        else if(decade== 2000)
        {
            questions = Questions.QuestionSetFrom2000s();

        }
        else if(decade== 2010)
        {
            questions = Questions.QuestionSetFrom2010s();

        }
        ImageIcon pic = new ImageIcon(questions[questionIndex].mediaFilePath);
        lblMediaPlayer.setIcon(pic);

        questionIndex = 0;

        lblDisplayQuestion.setText(questions[questionIndex].textQuestion);
    }

我补充说,这是试图解释我从哪里得到的问题

public class Questions 
{

    public static boolean AccountCreation(String userName, String password)
    {

        return true;
    }
    /**
     * method to return array of questions from chosen decade
     * @return
     */
    public static QuestionObject[] QuestionSetFrom60s()
    {
        QuestionObject pictureQuestion = new QuestionObject();
        pictureQuestion.textQuestion = "Name This Character";
        pictureQuestion.dataType =  0;
        pictureQuestion.rightAnswer = "charlie brown";
        pictureQuestion.mediaFilePath = "Charlie Brown.jpg";

        QuestionObject themeTuneQuestion = new QuestionObject();
        themeTuneQuestion.textQuestion = "Name This Theme Tune";
        themeTuneQuestion.dataType =  1;
        themeTuneQuestion.rightAnswer = "the waltons";
        themeTuneQuestion.mediaFilePath = "the waltons.wav";

        QuestionObject videoQuestion = new QuestionObject();
        videoQuestion.textQuestion = "Who had a hit with the song ? Are You Lonesome Tonight";
        videoQuestion.dataType =  2;
        videoQuestion.rightAnswer = "elvis presley";
        videoQuestion.mediaFilePath = "";

        QuestionObject questionArray [] = {pictureQuestion,themeTuneQuestion,videoQuestion};

        return questionArray;
    }
    /**
     * method to return array of questions from chosen decade
     * @return
     */
    public static QuestionObject[] QuestionSetFrom70s()
    {
        QuestionObject pictureQuestion = new QuestionObject();
        pictureQuestion.textQuestion = "Name This Character";
        pictureQuestion.dataType =  0;
        pictureQuestion.rightAnswer = "worzal gummidge";
        pictureQuestion.mediaFilePath = "worzal gummidge.jpg";

        QuestionObject themeTuneQuestion = new QuestionObject();
        themeTuneQuestion.textQuestion = "Name This Theme Tune";
        themeTuneQuestion.dataType =  1;
        themeTuneQuestion.rightAnswer = "black beauty";
        themeTuneQuestion.mediaFilePath = "the adventure of black beauty.wav";

        QuestionObject textQuestion = new QuestionObject();
        textQuestion.textQuestion = "Which Group Performed The Song SOS";
        textQuestion.dataType =  2;
        textQuestion.rightAnswer = "abba";
        textQuestion.mediaFilePath = "";

        QuestionObject questionArray [] = {pictureQuestion,themeTuneQuestion,textQuestion};

        return questionArray;
    }
    /**
     * method to return array of questions from chosen decade
     * @return
     */
    public static QuestionObject[] QuestionSetFrom80s()
    {
        QuestionObject pictureQuestion = new QuestionObject();
        pictureQuestion.textQuestion = "Name This Character";
        pictureQuestion.dataType =  0;
        pictureQuestion.rightAnswer = "falcor";
        pictureQuestion.mediaFilePath = "Falcor.jpg";

        QuestionObject themeTuneQuestion = new QuestionObject();
        themeTuneQuestion.textQuestion = "Name This Theme Tune";
        themeTuneQuestion.dataType =  1;
        themeTuneQuestion.rightAnswer = "et";
        themeTuneQuestion.mediaFilePath = "ET.wav";

        QuestionObject videoQuestion = new QuestionObject();
        videoQuestion.textQuestion = "Who had the hit Beat It in 1982";
        videoQuestion.dataType =  2;
        videoQuestion.rightAnswer = "michael jackson";
        videoQuestion.mediaFilePath = "";

        QuestionObject questionArray [] = {pictureQuestion,themeTuneQuestion,videoQuestion};

        return questionArray;
    }
    /**
     * method to return array of questions from chosen decade
     * @return
     */
    public static QuestionObject[] QuestionSetFrom90s()
    {

        QuestionObject pictureQuestion = new QuestionObject();
        pictureQuestion.textQuestion = "Name This Character";
        pictureQuestion.dataType =  0;
        pictureQuestion.rightAnswer = "tommy pickles";
        pictureQuestion.mediaFilePath = "tommy pickles.jpg";

        QuestionObject themeTuneQuestion = new QuestionObject();
        themeTuneQuestion.textQuestion = "Name This Theme Tune";
        themeTuneQuestion.dataType =  1;
        themeTuneQuestion.rightAnswer = "the crystal maze";
        themeTuneQuestion.mediaFilePath = "the crystal maze.wav";

        QuestionObject videoQuestion = new QuestionObject();
        videoQuestion.textQuestion = "Which 90's Sitcom Featured 6 Friends That Sat Around In A Coffee Shop?";
        videoQuestion.dataType =  2;
        videoQuestion.rightAnswer = "friends";
        videoQuestion.mediaFilePath = "";


        QuestionObject questionArray [] = {pictureQuestion,themeTuneQuestion,videoQuestion};

        return questionArray;
    }
    /**
     * method to return array of questions from chosen decade
     * @return
     */
    public static QuestionObject[] QuestionSetFrom2000s()
    {


        QuestionObject pictureQuestion = new QuestionObject();
        pictureQuestion.textQuestion = "Name This Character";
        pictureQuestion.dataType =  0;
        pictureQuestion.rightAnswer = "walter white";
        pictureQuestion.mediaFilePath = "walt.jpg";

        QuestionObject themeTuneQuestion = new QuestionObject();
        themeTuneQuestion.textQuestion = "Name This Theme Tune";
        themeTuneQuestion.dataType =  1;
        themeTuneQuestion.rightAnswer = "two and a half men";
        themeTuneQuestion.mediaFilePath = "two.wav";

        QuestionObject videoQuestion = new QuestionObject();
        videoQuestion.textQuestion = "What is the main character of the sopranos";
        videoQuestion.dataType =  2;
        videoQuestion.rightAnswer = "tony";
        videoQuestion.mediaFilePath = "";

        QuestionObject questionArray [] = {pictureQuestion,themeTuneQuestion,videoQuestion};

        return questionArray;
    }
    /**
     * method to return array of questions from chosen decade
     * @return
     */
    public static QuestionObject[] QuestionSetFrom2010s()
    {


        QuestionObject pictureQuestion = new QuestionObject();
        pictureQuestion.textQuestion = "Name This Character";
        pictureQuestion.dataType =  0;
        pictureQuestion.rightAnswer = "elsa";
        pictureQuestion.mediaFilePath = "frozen.jpg";

        QuestionObject themeTuneQuestion = new QuestionObject();
        themeTuneQuestion.textQuestion = "Name This Theme Tune";
        themeTuneQuestion.dataType =  1;
        themeTuneQuestion.rightAnswer = "game of thrones";
        themeTuneQuestion.mediaFilePath = "Game.wav";

        QuestionObject textQuestion = new QuestionObject();
        textQuestion.textQuestion = "Who starred in the 2013 version of house of cards";
        textQuestion.dataType =  2;
        textQuestion.rightAnswer = "kevin spacey";
        textQuestion.mediaFilePath = "";

        QuestionObject questionArray [] = {pictureQuestion,themeTuneQuestion,textQuestion};

        return questionArray;
    }

}//end of class

共有1个答案

韶浩博
2023-03-14

要测试某些代码,首先需要使其具有可测试性。您听说过测试驱动删除(TDD)吗?

在您提供的示例中,您可能无法修改代码。如果您可以将选择问题集的逻辑提取到一个单独的方法中,并将其作为返回值的方法进行测试。

如果您不能修改代码,我认为您应该能够查询GamePlay类实例的“Questions”字段。我希望您有类似于getQuestions方法或者字段本身是public/protected/default--因此junit测试用例可以访问。

 类似资料:
  • 我正在尝试创建我的第一个测试。我必须证明一个方法返回一个ContextLambda类型,我正在使用assertSame函数测试它,但是我的测试失败了,我不知道用什么assert来测试这个,用assertEquals也失败了。我的测试是这样的:

  • 问题内容: 测试(使用反射)最简单的方法是,给定方法(即java.lang.Method实例)是否具有返回类型,可以安全地强制转换为List ? 考虑以下代码段: 所有方法1、2、3均满足要求。对于method1进行测试非常容易(通过getGenericReturnType(),该方法返回ParameterizedType的实例),但是对于method2和3来说,并不是那么明显。我想通过遍历所有g

  • 我有这样的抽象类: 我还有一个扩展抽象的类:

  • 实际上,它的测试如果eat方法对宠物有效,但我也需要检查feedPet方法对玩家也有效。 任何想法或建议都非常感谢。

  • 新的Spring靴。 控制器中的API看起来像, 测试用例看起来像, 现在,在运行测试用例(命令1)后,我得到了以下结果 “java.lang.AssertionError:预期状态: 但“命令2”如期成功。 我的问题是, RestController Prefix Path Controller Prefix Path=整个路径。 为了调用API,我们必须遵循上面的格式,但是如果遵循相同的内容,