如何使用eclipse进行junit测试

2024-11-03 13:45:12

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

如何使用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测试

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

如何使用eclipse进行junit测试
猜你喜欢