Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -36,19 +36,25 @@ 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

- 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.
- 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.
27 changes: 27 additions & 0 deletions src/main/java/Cohort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.ArrayList;
import java.util.List;

public class Cohort {

private List<Student> 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<Student> getStudents() {
return students;
}
}
38 changes: 38 additions & 0 deletions src/main/java/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayList;

public class Student {

private long id;
private String name;
private ArrayList<Integer> grades;

public Student(long id, String name) {
this.id = id;
this.name = name;
this.grades = new ArrayList<>();
}

public long getId() {
return this.id;
}

public String getName() {
return this.name;
}

public ArrayList<Integer> getGrades() {
return this.grades;
}

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() ;
}
}
56 changes: 56 additions & 0 deletions src/test/java/CohortTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
41 changes: 41 additions & 0 deletions src/test/java/StudentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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);
}

@Test
public void testStudentFields(){
Student fer = new Student(1L, "fer");
assertSame(1L, fer.getId());
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));
}

@Test
public void testAverageGrade(){
Student fer = new Student(1L, "fer");
fer.addGrade(100);
fer.addGrade(80);
assertEquals(90, fer.getGradeAverage(), 0);
}
}