Issue
I think this is the same issue that was flagged in Testing abstract class throws InstantiationException but I don't know whether my fix is the same as the one suggested there. In any case, the answer didn't lead me to it, so I thought it might help someone else if it is right (I don't have the reputation to comment there yet).
I have the abstract test class 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);
}
}
and CourseSessionTest.java that extends from it.
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);
}
}
My AllTests.java is a 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;
}
}
I added console logs at the start of each method to see if I could suss out what was going on, which led me to take SessionTest.class out of the suite, and that made the tests pass.
I just want to check whether I'm right not to have SessionTest in the suite. There are a lot of methods in Session.java and obviously I don't want to make my tests pass by taking out the failing ones. But equally, given that you can't instantiate abstract classes directly, presumably it is right to only test the subclasses and the failure here is that I'm trying to instantiate the abstract class?
Solution
As per Progman, the problem here is that by adding SessionTest.class to my TestSuite, I am having junit attempt to instantiate an abstract class. There is no point in doing this anyway, since all of the methods in SessionTest are called by CourseSessionTest. Taking SessionTest.class out of the TestSuite solved the problem.
Answered By - Anders
Answer Checked By - Gilberto Lyons (JavaFixing Admin)