import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertion
ID: 3746402 • Letter: I
Question
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class StudentTest { // Test that a class gets added to the student's Classes list // Create a student, add a class, and make sure the class that // was added exists in the student's classes. @Test public void testAddClass(){ // Arrange // Act // Assert fail("Implement this test method"); } // Test that a class does not get added if maxNumberOfClasses // has been reached. Create a student with a max number of classes // of 2, add three classes, make sure the first two were added but // the third was not. @Test public void testAddClassDoesntAddClassesIfMaxed(){ // Arrange // Act // Assert fail("Implement this test method"); } // Test the formatting of the schedule when getFormattedSchedule is called. // Add a couple classes to student, then make sure the string returned by // getFormattedSchedule is in the format {ClassName1} {ClassName2} @Test public void testGetFormattedSchedule(){ // Arrange // Act // Assert fail("Implement this test method"); } // Test that if a student has no classes, when getFormattedSchedule is called // the string "No classes added." gets returned. @Test public void testGetFormattedScheduleReturnsNoClassesAddedIfNoClasses(){ // Arrange // Act // Assert fail("Implement this test method"); } }
Explanation / Answer
import static org.junit.Assert.*; import org.junit.Test; public class StudentTest { @Test public void testDefaultStudent() { Student student = new Student(2); assertNotNull(student); assertNotNull(student.getClasses()); assertEquals(0, student.getClasses().size()); } @Test public void testDefaultSchedule() { Student student = new Student(2); assertEquals("No classes added.", student.getFormattedSchedule()); } @Test public void testStudentWithClasses() { Class a = new Class("Course1"); Class b = new Class("Course2"); Student student = new Student(2); student.addClass(a); student.addClass(b); assertEquals(2, student.getClasses().size()); assertEquals(a, student.getClasses().get(0)); assertEquals(b, student.getClasses().get(1)); } @Test public void testMaxClasses() { Class a = new Class("Course1"); Class b = new Class("Course2"); Class c = new Class("Course3"); // creating with maximum classes 2 Student student = new Student(2); student.addClass(a); student.addClass(b); assertEquals("The number of classes should be 2.", 2, student.getClasses().size()); student.addClass(c); // should not be added assertEquals("There should be only 2 maximum classes.", 2, student.getClasses().size()); } @Test public void testGetFormattedSchedule() { Class a = new Class("Course1"); Class b = new Class("Course2"); Student student = new Student(2); student.addClass(a); student.addClass(b); String fomattedSchedule = "Course1" + " " + "Course2" + " "; assertEquals(fomattedSchedule, student.getFormattedSchedule()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.