From 9425d0bb4a093fc4ce76b8f9e9157d7c5b9d3c4f Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 4 Sep 2019 16:16:01 -0500 Subject: [PATCH 01/11] test: if a Student instance can be created --- src/main/java/Student.java | 16 ++++++++++++++++ src/test/java/StudentTest.java | 15 +++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/main/java/Student.java create mode 100644 src/test/java/StudentTest.java diff --git a/src/main/java/Student.java b/src/main/java/Student.java new file mode 100644 index 00000000..e06700a7 --- /dev/null +++ b/src/main/java/Student.java @@ -0,0 +1,16 @@ +import java.util.ArrayList; + +public class Student { + + private long id; + private String name; + private ArrayList grades; + + public Student(long id, String name) { + this.id = id; + this.name = name; + this.grades = new ArrayList<>(); + } + + +} diff --git a/src/test/java/StudentTest.java b/src/test/java/StudentTest.java new file mode 100644 index 00000000..21b7298c --- /dev/null +++ b/src/test/java/StudentTest.java @@ -0,0 +1,15 @@ +import org.junit.Test; + +import java.util.ArrayList; + +import static org.junit.Assert.*; + +public class StudentTest { + @Test + public void testCreateStudent(){ + Student fer = new Student(1L, "fer"); + Student ryan = null; + assertNull(ryan); + assertNotNull(fer); + } +} \ No newline at end of file From 474d9935dd67c914d47de95867eafe6af50267f1 Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 4 Sep 2019 16:17:57 -0500 Subject: [PATCH 02/11] test: checks if Student instance can return those values via getters --- src/main/java/Student.java | 11 +++++++++++ src/test/java/StudentTest.java | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/Student.java b/src/main/java/Student.java index e06700a7..49a597b4 100644 --- a/src/main/java/Student.java +++ b/src/main/java/Student.java @@ -12,5 +12,16 @@ public Student(long id, String name) { this.grades = new ArrayList<>(); } + public long getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public ArrayList getGrades() { + return this.grades; + } } diff --git a/src/test/java/StudentTest.java b/src/test/java/StudentTest.java index 21b7298c..b1557fba 100644 --- a/src/test/java/StudentTest.java +++ b/src/test/java/StudentTest.java @@ -12,4 +12,12 @@ public void testCreateStudent(){ assertNull(ryan); assertNotNull(fer); } + + @Test + public void testStudentFields(){ + Student fer = new Student(1L, "fer"); + assertSame(1L, fer.getId()); + assertSame("fer", fer.getName()); + assertSame(0, fer.getGrades().size()); + } } \ No newline at end of file From 38e920ae6696b93f343265d32388c7b3e9332a6f Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 4 Sep 2019 16:18:32 -0500 Subject: [PATCH 03/11] test: AddGrade functionality --- src/main/java/Student.java | 4 ++++ src/test/java/StudentTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/main/java/Student.java b/src/main/java/Student.java index 49a597b4..3c61835e 100644 --- a/src/main/java/Student.java +++ b/src/main/java/Student.java @@ -24,4 +24,8 @@ public ArrayList getGrades() { return this.grades; } + public void addGrade(int grade) { + this.grades.add(grade); + } + } diff --git a/src/test/java/StudentTest.java b/src/test/java/StudentTest.java index b1557fba..36ff14bf 100644 --- a/src/test/java/StudentTest.java +++ b/src/test/java/StudentTest.java @@ -20,4 +20,14 @@ public void testStudentFields(){ assertSame("fer", fer.getName()); assertSame(0, fer.getGrades().size()); } + + + @Test + public void testAddGrade(){ + Student fer = new Student(1L, "fer"); + fer.addGrade(100); + assertSame(100, fer.getGrades().get(0)); + fer.addGrade(80); + assertSame(80, fer.getGrades().get(1)); + } } \ No newline at end of file From 602352877667a0e32210467f180a4b1a6ca84be6 Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 4 Sep 2019 16:20:34 -0500 Subject: [PATCH 04/11] test: averageGrade functionality --- src/main/java/Student.java | 7 +++++++ src/test/java/StudentTest.java | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/Student.java b/src/main/java/Student.java index 3c61835e..507d664c 100644 --- a/src/main/java/Student.java +++ b/src/main/java/Student.java @@ -28,4 +28,11 @@ public void addGrade(int grade) { this.grades.add(grade); } + public double getGradeAverage() { + double sum = 0; + for (int grade: this.grades) { + sum += grade; + } + return sum / this.grades.size() ; + } } diff --git a/src/test/java/StudentTest.java b/src/test/java/StudentTest.java index 36ff14bf..a3cfb11f 100644 --- a/src/test/java/StudentTest.java +++ b/src/test/java/StudentTest.java @@ -30,4 +30,12 @@ public void testAddGrade(){ fer.addGrade(80); assertSame(80, fer.getGrades().get(1)); } + + @Test + public void testAverageGrade(){ + Student fer = new Student(1L, "fer"); + fer.addGrade(100); + fer.addGrade(80); + assertEquals(90, fer.getGradeAverage(), 0); + } } \ No newline at end of file From 27d0b9bd5132b6e948bbdd1e3d30e7d99314afd5 Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 4 Sep 2019 16:41:57 -0500 Subject: [PATCH 05/11] feat: add Cohort class --- src/main/java/Cohort.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/Cohort.java diff --git a/src/main/java/Cohort.java b/src/main/java/Cohort.java new file mode 100644 index 00000000..4baa092f --- /dev/null +++ b/src/main/java/Cohort.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +import java.util.List; + +public class Cohort { + + private List students; + + public Cohort() { + this.students = new ArrayList<>(); + } + + public double getCohortAverage() { + double avg = 0; + for (Student student: this.getStudents()) { + avg += student.getGradeAverage(); + } + return avg / this.getStudents().size(); + } + + public void addStudent(Student student){ + students.add(student); + } + + public List getStudents() { + return students; + } +} \ No newline at end of file From 9df249f2218244f6099df2bff7f737f126674282 Mon Sep 17 00:00:00 2001 From: Fernando Mendoza Date: Mon, 16 Dec 2019 15:48:02 -0600 Subject: [PATCH 06/11] fix: sync readme files with master --- README.md | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9c54d970..1b078789 100644 --- a/README.md +++ b/README.md @@ -12,16 +12,16 @@ ## Exercises -### Part One: The Test Driven Cycle (Red, Green, Refactor) +### Part One: The Test Driven Development Cycle (Red, Green, Refactor) -We will follow the best practices of TDD and we will create our tests first and write production code once we see the test fail. We will create a `Student` and a `StudentTest` class for a grades application, let's analyse the requirements for the `Student` class: +We will follow the best practices of TDD and we will create our tests first and write production code once we see the test fail. We will create a `Student` and a `StudentTest` class for a grades application, before you start let's analyse the requirements for the `Student` class: - `id` should be a `long` number used to represent a "unique user" in our application. - `name` is a `String` that holds the name of the student. - `grades` is an `ArrayList` that contains a list of `Integer` numbers. -1. Create a new branch called `part-one` and read carefully the next instructions. -1. Create a `StudentTest.java` class file inside of `src/test/java` and remember to write the test before the actual production code. We will simulate the `C(reate) R(ead)` from the `CRUD` functionality in our grades application, you should be able to test and create the following requirements: +1. Create a new branch called `students-tests` and read carefully the next instructions. +1. Create a `StudentTest.java` class file inside of `src/test/java` (you might have to create these folders yourself) and remember to write the test before the actual production code. We will simulate the `C(reate) R(ead)` from the `CRUD` functionality in our grades application, you should be able to test and create the following requirements: - The `Student` class should have a constructor that sets both the name and id property, it initializes the grades property as an empty ArrayList. - The `Student` class should have the following methods: @@ -38,17 +38,23 @@ We will follow the best practices of TDD and we will create our tests first and 1. As always, commit and push all your changes once you're done. _At the end of the exercise you will ended up with a `Student.java` and a `StudentTest.java` class._ +#### Bonus + +- Go ahead and try to add the rest of the `CRUD` tests and functionality, write the methods for `updateGrade()` and `deleteGrade()` in the `Student` class. + ### Part Two: Testing Legacy Code -Once you finished the `Student.java` class. +Once you are done with the `Student.java` class. -1. Checkout to the `part-two` branch, there you will find a `Cohort.java` class inside the `src/main/java` folder, this class has already a lot of methods to calculate the cohort average grade and add a student. Let's go ahead and make sure there's sufficient tests for this class to be deploy to production: +1. Checkout to the `cohorts-feature` branch, there you will find a `Cohort.java` class inside the `src/main/java` folder, this class has already a lot of methods to calculate the cohort average grade and add a student. Let's go ahead and make sure there's sufficient tests for this class to be deployed to production: -1. Create a test for each of the following concerns and code any extra tests as needed. - - A Cohort instance always have students before being used. - - A Cohort instance can add a `Student` to the `List` of students. - - A Cohort instance can get the list of students. +1. Start by creating a new branch called: `cohorts-tests`. +1. Then create a `CohortTest` class and build a test for each of the following requirements: + - A `Cohort` instance can add a `Student` to the `List` of students. + - A `Cohort` instance can get the current `List` of students. + - A `Cohort` instance can get the average and it's being calculated correctly. +1. Go back to the `StudentTest` class and refactor the code to avoid any repetition in the test cases, the use of the `@Before` annotation will be useful to achieve this, do the same with this new `CohortTest` class if you find any code repetition as well. -### Bonus +#### Bonus -- Go ahead and try to add the rest of the `CRUD` tests and functionality, write the methods for `updateGrade()` and `deleteGrade()` in the `Student` class. \ No newline at end of file +- Follow the TDD cycle and create a new feature to find students by their ID like: `findStudentById(long id)` in the `Cohort` class, remember to create the tests first before any production code. \ No newline at end of file From dfe6bae317fad5f84172e1b9d9745c8bd91e26a8 Mon Sep 17 00:00:00 2001 From: Fernando Mendoza Date: Tue, 17 Dec 2019 10:03:02 -0600 Subject: [PATCH 07/11] feat: create tests for Cohort features --- src/test/java/CohortTest.java | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/CohortTest.java diff --git a/src/test/java/CohortTest.java b/src/test/java/CohortTest.java new file mode 100644 index 00000000..2c3e28f0 --- /dev/null +++ b/src/test/java/CohortTest.java @@ -0,0 +1,40 @@ +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import static org.junit.Assert.*; + +public class CohortTest { + + + @Test + public void testAddStudentAndGetStudentsWork(){ + Cohort cohortWithOne = new Cohort(); + assertEquals(0, cohortWithOne.getStudents().size()); + cohortWithOne.addStudent(new Student(1, "Fer M")); + assertEquals(1, cohortWithOne.getStudents().size()); + +// assertEquals(1, cohortWithOne.getStudents().get(0).getId()); + } + + @Test + public void testIfAvgIsCorrect(){ + Cohort cohortWithMany = new Cohort(); + + Student fer = new Student(1, "Fer M"); + fer.addGrade(90); + + Student trant = new Student(2, "Trant B"); + trant.addGrade(100); + trant.addGrade(100); + trant.addGrade(100); + + cohortWithMany.addStudent(fer); + cohortWithMany.addStudent(trant); + + assertEquals(95.0, cohortWithMany.getCohortAverage(), 0); + + } + + +} \ No newline at end of file From 0cd79fbdd04bba842f5f687c7b7a63225ea81577 Mon Sep 17 00:00:00 2001 From: Fernando Mendoza Date: Tue, 17 Dec 2019 10:10:39 -0600 Subject: [PATCH 08/11] feat: clean up the test --- src/test/java/CohortTest.java | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/test/java/CohortTest.java b/src/test/java/CohortTest.java index 2c3e28f0..be203543 100644 --- a/src/test/java/CohortTest.java +++ b/src/test/java/CohortTest.java @@ -1,3 +1,4 @@ +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; @@ -6,20 +7,15 @@ public class CohortTest { + Cohort emptyCohort; + Cohort cohortWithOne; + Cohort cohortWithMany; - @Test - public void testAddStudentAndGetStudentsWork(){ - Cohort cohortWithOne = new Cohort(); - assertEquals(0, cohortWithOne.getStudents().size()); - cohortWithOne.addStudent(new Student(1, "Fer M")); - assertEquals(1, cohortWithOne.getStudents().size()); - -// assertEquals(1, cohortWithOne.getStudents().get(0).getId()); - } - - @Test - public void testIfAvgIsCorrect(){ - Cohort cohortWithMany = new Cohort(); + @Before + public void setup(){ + emptyCohort = new Cohort(); + cohortWithOne = new Cohort(); + cohortWithMany = new Cohort(); Student fer = new Student(1, "Fer M"); fer.addGrade(90); @@ -29,12 +25,21 @@ public void testIfAvgIsCorrect(){ trant.addGrade(100); trant.addGrade(100); + cohortWithOne.addStudent(fer); cohortWithMany.addStudent(fer); cohortWithMany.addStudent(trant); + } - assertEquals(95.0, cohortWithMany.getCohortAverage(), 0); - + @Test + public void testAddStudentAndGetStudentsWork(){ + assertEquals(0, emptyCohort.getStudents().size()); + assertEquals(1, cohortWithOne.getStudents().size()); +// assertEquals(1, cohortWithOne.getStudents().get(0).getId()); } + @Test + public void testIfAvgIsCorrect(){ + assertEquals(95.0, cohortWithMany.getCohortAverage(), 0); + } } \ No newline at end of file From 7117e51e3ffeca1ec2395772ad8ea1f719d4c9d5 Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 1 Jul 2020 14:44:59 -0500 Subject: [PATCH 09/11] fix: typos --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1b078789..e2832d37 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ### Part One: The Test Driven Development Cycle (Red, Green, Refactor) -We will follow the best practices of TDD and we will create our tests first and write production code once we see the test fail. We will create a `Student` and a `StudentTest` class for a grades application, before you start let's analyse the requirements for the `Student` class: +We will follow the best practices of TDD, and we will create our tests first and write production code once we see the test fail. We will create a `Student` and a `StudentTest` class for a grades application, before you start let's analyse the requirements for the `Student` class: - `id` should be a `long` number used to represent a "unique user" in our application. - `name` is a `String` that holds the name of the student. @@ -36,7 +36,7 @@ We will follow the best practices of TDD and we will create our tests first and public double getGradeAverage(); ``` 1. As always, commit and push all your changes once you're done. -_At the end of the exercise you will ended up with a `Student.java` and a `StudentTest.java` class._ +_At the end of the exercise you will end up with a `Student.java` and a `StudentTest.java` class._ #### Bonus @@ -52,7 +52,7 @@ Once you are done with the `Student.java` class. 1. Then create a `CohortTest` class and build a test for each of the following requirements: - A `Cohort` instance can add a `Student` to the `List` of students. - A `Cohort` instance can get the current `List` of students. - - A `Cohort` instance can get the average and it's being calculated correctly. + - A `Cohort` instance can get the average, and it's being calculated correctly. 1. Go back to the `StudentTest` class and refactor the code to avoid any repetition in the test cases, the use of the `@Before` annotation will be useful to achieve this, do the same with this new `CohortTest` class if you find any code repetition as well. #### Bonus From bf69256a91e62090521fea8a2c8401be097d722b Mon Sep 17 00:00:00 2001 From: Fer Date: Wed, 1 Jul 2020 14:45:21 -0500 Subject: [PATCH 10/11] fix: remove solution --- src/test/java/CohortTest.java | 45 ----------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 src/test/java/CohortTest.java diff --git a/src/test/java/CohortTest.java b/src/test/java/CohortTest.java deleted file mode 100644 index be203543..00000000 --- a/src/test/java/CohortTest.java +++ /dev/null @@ -1,45 +0,0 @@ -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; -import static org.junit.Assert.*; - -public class CohortTest { - - Cohort emptyCohort; - Cohort cohortWithOne; - Cohort cohortWithMany; - - @Before - public void setup(){ - emptyCohort = new Cohort(); - cohortWithOne = new Cohort(); - cohortWithMany = new Cohort(); - - Student fer = new Student(1, "Fer M"); - fer.addGrade(90); - - Student trant = new Student(2, "Trant B"); - trant.addGrade(100); - trant.addGrade(100); - trant.addGrade(100); - - cohortWithOne.addStudent(fer); - cohortWithMany.addStudent(fer); - cohortWithMany.addStudent(trant); - } - - @Test - public void testAddStudentAndGetStudentsWork(){ - assertEquals(0, emptyCohort.getStudents().size()); - assertEquals(1, cohortWithOne.getStudents().size()); -// assertEquals(1, cohortWithOne.getStudents().get(0).getId()); - } - - @Test - public void testIfAvgIsCorrect(){ - assertEquals(95.0, cohortWithMany.getCohortAverage(), 0); - } - -} \ No newline at end of file From 5591b0509368050084932da482217c82c77028f2 Mon Sep 17 00:00:00 2001 From: Eleanor Ellingson Date: Thu, 7 Jan 2021 17:06:33 -0600 Subject: [PATCH 11/11] Tested Cohort class --- src/test/java/CohortTest.java | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/CohortTest.java diff --git a/src/test/java/CohortTest.java b/src/test/java/CohortTest.java new file mode 100644 index 00000000..98fff723 --- /dev/null +++ b/src/test/java/CohortTest.java @@ -0,0 +1,56 @@ +import org.junit.Before; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static org.junit.Assert.*; + +public class CohortTest { + Cohort emptyCohort; + Cohort cohortWithOne; + Cohort cohortWithMany; + + @Before + public void setUp(){ + emptyCohort = new Cohort(); + cohortWithOne = new Cohort(); + cohortWithMany = new Cohort(); + + Student jill = new Student(1, "Jill Jack"); + jill.addGrade(80); + + Student bob = new Student(2, "Bob Smith"); + bob.addGrade(100); + bob.addGrade(100); + bob.addGrade(100); + + cohortWithOne.addStudent(jill); + cohortWithMany.addStudent(jill); + cohortWithMany.addStudent(bob); + + } + + @Test + public void testAddStudent(){ + cohortWithMany.addStudent(new Student(3, "John Doe")); + + assertEquals("John Doe", cohortWithMany.getStudents().get(2).getName()); + + } + + @Test + public void testCurrentList(){ + assertEquals(0, emptyCohort.getStudents().size()); + assertEquals(1, cohortWithOne.getStudents().size()); + + assertEquals(1, cohortWithMany.getStudents().get(0).getId()); + assertEquals(2, cohortWithMany.getStudents().get(1).getId()); + } + + @Test + public void testGetAverage(){ + assertEquals(Double.NaN, emptyCohort.getCohortAverage(), 0); + assertEquals(80, cohortWithOne.getCohortAverage(), 0); + assertEquals(90, cohortWithMany.getCohortAverage(),0); + } + +}