【发布时间】: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
【问题讨论】:
-
可能重复:stackoverflow.com/questions/16043819/junit-testing-void-methods。但是在您发布的情况下,您实际上没有 void 方法。您发布了一个构造函数。