我认为这与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可以解决此问题。

07-27 16:23