gpt4 book ai didi

java - 无法在 JUnit 中实例化测试用例

转载 作者:行者123 更新时间:2023-12-02 09:08:37 25 4
gpt4 key购买 nike

我认为这与 Testing abstract class throws InstantiationException 中标记的问题相同但我不知道我的修复是否与那里建议的相同。无论如何,答案并没有引导我找到它,所以我认为如果它是正确的,它可能会帮助其他人(我还没有在那里发表评论的声誉)。

我有抽象测试类SessionTest.java:

package sis.studentinfo;

import junit.framework.TestCase;
import java.util.*;
import static sis.studentinfo.DateUtil.createDate;

abstract public class SessionTest extends TestCase {
private Session session;
private Date startDate;
private static final int CREDITS = 3;

public void setUp(){
startDate = createDate(2003, 1, 6);
session = createSession("ENGL", "101", startDate);
session.setNumberOfCredits(CREDITS);
}

abstract protected Session createSession(String department, String number, Date startDate);

public void testCreate(){
System.out.println("Test create");
assertEquals("ENGL", session.getDepartment());
assertEquals("101", session.getNumber());
assertEquals(0, session.getNumberOfStudents());
assertEquals(startDate, session.getStartDate());
}

public void testEnrollStudents(){
System.out.println("Test enroll students");
Student student1 = new Student("Cain DiVoe");
session.enroll(student1);
assertEquals(CREDITS, student1.getCredits());
assertEquals(1, session.getNumberOfStudents());
assertEquals(student1, session.get(0));

Student student2 = new Student("Coralee DeVaughn");
session.enroll(student2);
assertEquals(CREDITS, student2.getCredits());
assertEquals(2, session.getNumberOfStudents());
assertEquals(student1, session.get(0));
assertEquals(student2, session.get(1));
}

public void testComparable(){
System.out.println("Test comparable");
final Date date = new Date();
Session sessionA = createSession("CMSC", "101", date);
Session sessionB = createSession("ENGL", "101", date);
assertTrue(sessionA.compareTo(sessionB) < 0);
assertTrue(sessionB.compareTo(sessionA) > 0);

Session sessionC = createSession("CMSC", "101", date);
assertEquals(0, sessionA.compareTo(sessionC));

Session sessionD = createSession("CMSC", "210", date);
assertTrue(sessionC.compareTo(sessionD) < 0);
assertTrue(sessionD.compareTo(sessionC) > 0);
}
}

以及从它扩展的 CourseSessionTest.java。

package sis.studentinfo;

import junit.framework.TestCase;
import java.util.*;
import static sis.studentinfo.DateUtil.createDate;

public class CourseSessionTest extends SessionTest {

public void testCourseDates(){
System.out.println("Test Course Dates");
Date startDate = DateUtil.createDate(2003, 1, 6);
Session session = createSession("ENGL", "200", startDate);
Date sixteenWeeksOut = createDate(2003, 4, 25);
assertEquals(sixteenWeeksOut, session.getEndDate());
}

public void testCount(){
System.out.println("Test Count");
CourseSession.resetCount();
createSession("", "", new Date());
assertEquals(1, CourseSession.getCount());
createSession("", "", new Date());
assertEquals(2, CourseSession.getCount());
}

protected Session createSession(String department, String number, Date date){
return CourseSession.create(department, number, date);
}

}

我的 AllTests.java 是一个 junit TestSuite:

package sis.studentinfo;

import junit.framework.TestSuite;

public class AllTests {
public static TestSuite suite(){
TestSuite suite = new TestSuite();
suite.addTestSuite(StudentTest.class);
suite.addTestSuite(DateUtilTest.class);
suite.addTestSuite(BasicGradingStrategyTest.class);
suite.addTestSuite(HonorsGradingStrategyTest.class);
suite.addTestSuite(SessionTest.class);
suite.addTestSuite(CourseSessionTest.class);
return suite;
}
}

我在每个方法的开头添加了控制台日志,以查看是否可以弄清楚发生了什么,这导致我将 SessionTest.class 从套件中取出,从而使测试通过。

我只是想检查一下我在套件中不使用 SessionTest 是否正确。 Session.java 中有很多方法,显然我不想通过删除失败的方法来使我的测试通过。但同样,鉴于您无法直接实例化抽象类,大概只测试子类是正确的,而这里的失败是我试图实例化抽象类?

最佳答案

根据 Progman,这里的问题是,通过将 SessionTest.class 添加到我的 TestSuite,我让 junit 尝试实例化一个抽象类。无论如何,这样做是没有意义的,因为 SessionTest 中的所有方法都是由 CourseSessionTest 调用的。从 TestSuite 中取出 SessionTest.class 解决了问题。

关于java - 无法在 JUnit 中实例化测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com