网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

如何使用eclipse进行junit测试

时间:2024-11-03 13:45:12

1、打开eclipse,点击左上角的File,新建一个project,命名为JunitTestDemo,然后在src目录下新建两个包,分别命名为StuScoreMS和Test。

如何使用eclipse进行junit测试

2、在StuScoreMS中新建一个class,命名为score.java。

如何使用eclipse进行junit测试

3、在score.java中输入如下代码:package StuScoreMS;import java.util.Scanner;public class Score { private static String result; private static String note; public void Checkscore(int score){ if(score<=100&&score>97){result="优秀";note="通过";} else if(score<=97&&score>94){result="分数很高";note="通过";} else if(score<=94&&score>92){result="高分";note="通过";} else if(score<=92&&score>88){result="很好";note="通过";} else if(score<=88&&score>85){result="好";note="通过";} else if(score<=85&&score>82){result="很满意";note="通过";} else if(score<=82&&score>79){result="满意";note="通过";} else if(score<=79&&score>74){result="一般";note="通过";} else if(score<=74&&score>50){result="可提高";note="通过";} else if(score<=50&&score>0){result="差";note="通过";} else {result="不确定";note="不确定";} }public String getResult(){ return result; }public static void main(String[] args) { String input; int score = 0; System.out.println("请输入你的成绩:"); Scanner reader = new Scanner(System.in); input = reader.nextLine(); try { score = Integer.parseInt(input); } catch (Exception e) { System.out.println("提示:你的输入有误!请检查输入是否正确!!!"); } Score stu = new Score(); stu.Checkscore(score); System.out.println("你的成绩评测结果为:" + result + "。备注:" + note + "。"); }}然后右击score.java,在选项new里面点击JUnit Test Case(如果没有该选项,请点击others,在JUnit 中选择JUnit Test Case),点击next,将Package改为Test,Name改为ScoreTest,点击next。

如何使用eclipse进行junit测试

4、进入Test Methods后,勾选Score中的Checkscore(int)和getResult()两个方法,点击finsh.

如何使用eclipse进行junit测试

5、新建了ScoreTest.java后,将两个方法里面的“fail("Not yet implemented"稆糨孝汶;);”删去,在testCheckscore()里面写上如下代码:score.Checkscore(70); assertEquals("可提高",score.getResult());在testGetResult()里面写上如下代码:score.Checkscore(40); assertEquals("差", score.getResult());

如何使用eclipse进行junit测试

6、保存后,右击ScoreTest.java,选择Run As,再选择Junit Test,即可运行junit,测试在ScoreTest.java里面的数据是否正确。如图,测试结果通过则显示绿条,否则显示红条,可以根据提示找到错误所在。

如何使用eclipse进行junit测试
© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com