diff --git a/exercises/grade-school/build.gradle b/exercises/grade-school/build.gradle index d019b23c7..7bd925eaf 100644 --- a/exercises/grade-school/build.gradle +++ b/exercises/grade-school/build.gradle @@ -7,6 +7,7 @@ repositories { } dependencies { + testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3' testCompile "junit:junit:4.12" } test { diff --git a/exercises/grade-school/src/test/java/SchoolTest.java b/exercises/grade-school/src/test/java/SchoolTest.java index 7bd2fc281..349d40653 100644 --- a/exercises/grade-school/src/test/java/SchoolTest.java +++ b/exercises/grade-school/src/test/java/SchoolTest.java @@ -1,14 +1,14 @@ import org.junit.Ignore; import org.junit.Test; -import java.lang.Integer; -import java.util.*; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.Map; +import java.util.List; +import java.util.Collection; +import org.hamcrest.Matcher; +import org.hamcrest.collection.IsIterableContainingInOrder; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -60,31 +60,29 @@ public void getsStudentsInEmptyGrade() { assertTrue(school.grade(1).isEmpty()); } - @Ignore - @Test - public void gradeReturnsStudentsInTheOrderTheyWereInserted() { - int grade = 4; - school.add("Bartimaeus", grade); - school.add("Nathaniel", grade); - school.add("Faquarl", grade); - List studentsInGrade = school.grade(grade); - assertThat(studentsInGrade.get(0), is("Bartimaeus")); - assertThat(studentsInGrade.get(1), is("Nathaniel")); - assertThat(studentsInGrade.get(2), is("Faquarl")); - } - @Ignore @Test public void sortsSchool() { + school.add("Kyle", 4); + school.add("Zed", 4); + school.add("Adam", 4); school.add("Jennifer", 4); school.add("Kareem", 6); school.add("Christopher", 4); - school.add("Kyle", 3); - Map> sortedStudents = new HashMap>(); - sortedStudents.put(6, Arrays.asList("Kareem")); - sortedStudents.put(4, Arrays.asList("Christopher", "Jennifer")); - sortedStudents.put(3, Arrays.asList("Kyle")); - assertEquals(school.studentsByGradeAlphabetical(), sortedStudents); + school.add("Kylie", 3); + Map sortedStudents = new HashMap(); + sortedStudents.put(6, IsIterableContainingInOrder + .contains("Kareem")); + sortedStudents.put(4, IsIterableContainingInOrder + .contains("Adam", "Christopher", "Jennifer", "Kyle", "Zed")); + sortedStudents.put(3, IsIterableContainingInOrder + .contains("Kylie")); + + Map schoolStudents = school.studentsByGradeAlphabetical(); + for (Map.Entry entry : sortedStudents.entrySet()) { + + assertThat((Collection) schoolStudents.get(entry.getKey()), entry.getValue()); + } } @Ignore @@ -93,8 +91,14 @@ public void modifyingFetchedGradeShouldNotModifyInternalDatabase() { String shouldNotBeAdded = "Should not be added to school"; int grade = 1; - List students = school.grade(grade); - students.add(shouldNotBeAdded); + Collection students = school.grade(grade); + + try { + students.add(shouldNotBeAdded); + } catch (Exception exception) { + // Also valid that the add operation throws an exception + // Such as UnsupportedOperationException when an umodifiable collection type is used + } assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded))); } @@ -107,9 +111,16 @@ public void modifyingSortedStudentsShouldNotModifyInternalDatabase() { List listWhichShouldNotBeAdded = new ArrayList<>(); listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded); - Map> sortedStudents = school.studentsByGradeAlphabetical(); - sortedStudents.put(grade,listWhichShouldNotBeAdded); + Map sortedStudents = school.studentsByGradeAlphabetical(); + + try { + sortedStudents.put(grade, listWhichShouldNotBeAdded); + } catch (Exception exception) { + // Also valid that the put operation throws an exception + // Such as UnsupportedOperationException when an unmodifiableMap is used + } - assertThat(school.studentsByGradeAlphabetical().get(grade), not(hasItem(studentWhichShouldNotBeAdded))); + assertThat(school.studentsByGradeAlphabetical().get(grade), + not(hasItem(studentWhichShouldNotBeAdded))); } }