【问题标题】:JUnit testing a java method,is it normal to test methods without a return type or should i use a different type of testing for them?JUnit测试java方法,测试没有返回类型的方法是否正常,或者我应该为它们使用不同类型的测试?
【发布时间】:2015-04-30 12:58:44
【问题描述】:

我正在测试一个程序作为大学作业的一部分,我测试了一些返回布尔值的方法,看看它们是否正确,我想知道用 JUnit 检查 void 方法是否是标准做法?还是有另一种方法。我将在此处发布其中一种方法作为示例。 谢谢

这是一个问答游戏程序,此方法接受一个 int,它定义了从中选择问题的年份。if 语句然后检查传入的年份并设置问题类中的问题(定义为对象) 不确定您是否需要知道这一点才能回答我的问题。

谢谢

  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

【问题讨论】:

标签: java testing junit


【解决方案1】:

要测试某些代码,您首先需要使其可测试。您是否听说过测试驱动开发 (TDD)?

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

如果您无法修改代码,我认为您应该能够查询 GamePlay 类实例的“问题”字段。我希望你有类似 getQuestions 方法的东西,或者字段本身是公共/受保护/默认的——因此 junit 测试用例可以访问。

在最坏的情况下,如果该字段是私有的并且没有任何公共 getter,那么您可以使用 Java 反射来访问该字段。但我相信这可能是相当“糟糕的做法”。

一旦您可以访问该领域,您就可以使用 Deh 提出的测试方法。

【讨论】:

    【解决方案2】:

    是的,这是正确的,因为您主要使用 JUnit 提供的 Assert 类来测试您的方法。

    @Test
    public void testQuestions() {
        Gameplay bean = new GamePlay(1960);
        Assert.assertNotNull(bean.getQuestions());
    }
    

    在上面的示例中,如果 questions 为空,那么 JUnit 将认为测试失败,您可以知道您的构造函数没有分配问题集。相反,如果它不为空,JUnit 会认为它是成功的。 assertNotNull 方法只是一个例子。 您可以找到更多示例 here.

    编辑:

    我编辑了我的示例以适应 OP 提供的构造函数。

    【讨论】:

    • OP 专门询问没有返回值的方法
    • 我得到一个错误 getQuestions() 说“方法 getQuestions() 未定义用于 GamePlay 类型”这是否意味着我需要在我的测试用例中包含 getQuestions 方法所在的类?
    • @Mick O'Gorman getQuestions() 代表问题的获取者。当我阅读您的构造函数时,问题似乎是一个类属性。
    • 我不认为我正在使用 get 方法,例如我正在使用问题对象,我已经从另一个类中添加了我的代码,如果它从我的问题中检索问题集有帮助吗?
    • 我把它改成了 assertNotNull(Questions.QuestionSetFrom60s());这似乎正在工作,因为当我将 assertNotNull 更改为 assertNull 时出现错误,所以我在这里确认我没有返回 null?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 2016-09-07
    • 2021-11-11
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多