idea測試類加載不出來怎麼辦?

CSDN問答 2022-01-08 04:25:09 阅读数:70

idea 不出

就像這樣,運行測試類後一直顯示加載,寫到main裏可以運行,同樣在eclipse測試類也可以。

package com.atguigu.exer;/* * 請把學生名與考試分數錄入到Set中,並按分數顯示前三名成績學員的名字.怎麼用Set實現? */import java.util.TreeSet;import java.util.Scanner;import org.junit.Test;public class ScoreTest { private Scanner scanner; @Test public void test1() { scanner = new Scanner(System.in); TreeSet<Student> treeSet = new TreeSet<>(); System.out.println("請輸入學生的姓名和成績,當輸入為負數時結束輸入"); for (int i = 1;; i++) { int score = 0; System.out.println("請輸入第" + i + "比特學生的姓名:"); String name = scanner.next(); System.out.println("請輸入第" + i + "比特學生的成績:(輸入負數,結束錄入)"); try { score = scanner.nextInt(); } catch (ClassCastException e) { // TODO: handle exception System.out.println("您輸入的分數有誤"); } if (score < 0) { break; } else { treeSet.add(new Student(name, score)); } } System.out.println("您輸入的成績生成成績單結果:"); for (Student student : treeSet) { System.out.println(student.getName() + "--->" + student.getScore()); } // 前三名學生的姓名 System.out.println("前三名學生的姓名為:"); int count = 0; for (Student student : treeSet) { if (count == 3) { break; } else { System.out.println(student.getName() + "--->" + student.getScore()); count++; } } }}class Student implements Comparable<Student> { private String name; private int score; public Student(String name, int score) { super(); this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + score; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (score != other.score) return false; return true; } @Override public int compareTo(Student o) { // TODO Auto-generated method stub int num = o.score - score; if (num != 0) { return num; } else { return this.name.compareTo(o.name); } }}



采納答案:

pom文件添加junit包,或者下載junit插件



其他答案2:

為了幫助解决問題,能不能截圖下ScoreTest測試類test1方法的方法體,或者直接截圖所有的ScoreTest類?如果能提供一下項目目錄結構更好。


其他答案3:

把測試類的代碼貼出來看看


其他答案4:
package com.atguigu.exer;/* * 請把學生名與考試分數錄入到Set中,並按分數顯示前三名成績學員的名字.怎麼用Set實現? */import java.util.TreeSet;import java.util.Scanner;import org.junit.Test;public class ScoreTest { private Scanner scanner; @Test public void test1() { scanner = new Scanner(System.in); TreeSet<Student> treeSet = new TreeSet<>(); System.out.println("請輸入學生的姓名和成績,當輸入為負數時結束輸入"); for (int i = 1;; i++) { int score = 0; System.out.println("請輸入第" + i + "比特學生的姓名:"); String name = scanner.next(); System.out.println("請輸入第" + i + "比特學生的成績:(輸入負數,結束錄入)"); try { score = scanner.nextInt(); } catch (ClassCastException e) { // TODO: handle exception System.out.println("您輸入的分數有誤"); } if (score < 0) { break; } else { treeSet.add(new Student(name, score)); } } System.out.println("您輸入的成績生成成績單結果:"); for (Student student : treeSet) { System.out.println(student.getName() + "--->" + student.getScore()); } // 前三名學生的姓名 System.out.println("前三名學生的姓名為:"); int count = 0; for (Student student : treeSet) { if (count == 3) { break; } else { System.out.println(student.getName() + "--->" + student.getScore()); count++; } } }}class Student implements Comparable<Student> { private String name; private int score; public Student(String name, int score) { super(); this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + score; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (score != other.score) return false; return true; } @Override public int compareTo(Student o) { // TODO Auto-generated method stub int num = o.score - score; if (num != 0) { return num; } else { return this.name.compareTo(o.name); } }}

其他答案5:

應該是idea本身的問題,解决方法就是不用scanner輸入或者

在你的Idea安裝目錄下的
D:\IntelliJ IDEA 2019.1.3\bin
裏面的文件 idea64.exe.vmoptions追加以下內容:
-Deditable.java.test.console=true;

重啟IDEA,重新執行單元測試類

版权声明:本文为[CSDN問答]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201080425085656.html