JUnit测试java方法,测试没有返回类型的方法是正常的还是应该使用不同的测试类型?
作为大学作业的一部分,我正在测试一个程序,我测试了一些返回布尔值的方法,看看它们是否正确,我想知道用JUnit检查void方法是否是标准做法?或者还有其他方法。我将在这里发布其中一种方法作为示例。 谢谢 这是一个问答游戏程序,此方法接受一个int,它定义了问题将从中选择的年份。if语句然后检查传入的年份,并从一个问题类(定义为对象)中设置问题,不确定您是否需要知道这一点才能回答我的问题 谢谢JUnit测试java方法,测试没有返回类型的方法是正常的还是应该使用不同的测试类型?,java,testing,junit,Java,Testing,Junit,作为大学作业的一部分,我正在测试一个程序,我测试了一些返回布尔值的方法,看看它们是否正确,我想知道用JUnit检查void方法是否是标准做法?或者还有其他方法。我将在这里发布其中一种方法作为示例。 谢谢 这是一个问答游戏程序,此方法接受一个int,它定义了问题将从中选择的年份。if语句然后检查传入的年份,并从一个问题类(定义为对象)中设置问题,不确定您是否需要知道这一点才能回答我的问题 谢谢 public GamePlay(int decade) { this();
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
是的,这是正确的,因为您主要使用JUnit提供的Assert类来测试您的方法
@Test
public void testQuestions() {
Gameplay bean = new GamePlay(1960);
Assert.assertNotNull(bean.getQuestions());
}
在上面的例子中,如果问题是空的,那么JUnit会考虑测试失败,你可以知道你的构造函数没有分配问题集。相反,如果它不是空的,JUnit会认为它是成功的。
assertNotNull方法只是一个示例。
您可以找到更多示例
编辑:
我编辑了我的示例,以适合OP提供的构造函数。来测试一些代码,您首先需要使其可测试。你听说过测试驱动的去开发(TDD)吗 在您提供的示例中,可能无法修改代码。如果您可以将选择问题集的逻辑提取到单独的方法中,并将其作为具有返回值的方法进行测试 如果您不能修改代码,我认为您应该能够查询GamePlay类实例的“questions”字段。我希望您有一些类似于getQuestions的方法,或者字段本身是public/protected/default,因此junit测试用例可以访问它 在最坏的情况下,如果字段是私有的,并且没有任何公共getter,那么您可以使用Java反射来访问该字段。但我相信这可能是一个相当“糟糕的练习”
一旦进入现场,您就可以使用Deh提出的测试方法。可能的重复:。但是在你发布的例子中,你实际上没有一个void方法。您发布了一个构造函数。OP专门询问没有返回值的方法。我收到一个错误,getQuestions()说“getQuestions()方法对于游戏类型未定义”这是否意味着我需要在测试用例中包含getQuestions方法所在的类?@Mick O'Gorman getQuestions()代表获得者回答问题。当我阅读您的构造函数时,问题似乎是一个类属性。我不认为我使用的是get方法,就像我使用的是问题对象一样,我已经从另一个类中添加了代码,如果有帮助的话,我可以从该类中检索问题集?我将其更改为assertNotNull(questions.QuestionSetFrom60s());当我将assertNotNull更改为assertNull时,我得到了一个错误,所以我在这里确认我没有得到返回的null?