- Fork this repository to make a copy on your own GitHub account.
- Make sure that your browser is showing this project in your own repositories list in your own account.
- Click the green button on the right that says "Clone or Download".
- The clone address should look like
git@github.com:your-github-username/junit-tests.git, whereyour-github-usernameis actually your own username on GitHub. - Once you've copied your repo's clone address, it's time to clone the project in one of two ways:
- If you're using IntelliJ, choose New->Project From Version Control->Git and then paste in the clone address.
git clone git@github.com:your-github-username/junit-tests.git. - If you're using command line, then execute the following command line command:
git clone git@github.com:your-github-username/junit-tests.git.
- If you're using IntelliJ, choose New->Project From Version Control->Git and then paste in the clone address.
- Once cloned to your projects directory, open up the project.
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:
idshould be alongnumber used to represent a "unique user" in our application.nameis aStringthat holds the name of the student.gradesis anArrayListthat contains a list ofIntegernumbers.
-
Create a new branch called
students-testsand read carefully the next instructions. -
Create a
StudentTest.javaclass file inside ofsrc/test/java(you might have to create these folders yourself) and remember to write the test before the actual production code. We will simulate theC(reate) R(ead)from theCRUDfunctionality in our grades application, you should be able to test and create the following requirements:- The
Studentclass should have a constructor that sets both the name and id property, it initializes the grades property as an empty ArrayList. - The
Studentclass should have the following methods:
- The
// returns the student's id
public long getId(){...}
// returns the student's name
public String getName(){...}
// adds the given grade to the grades list
public void addGrade(int grade){...}
// returns the list of grades
public ArrayList<Integer> getGrades(){...}
// returns the average of the students grades
public double getGradeAverage(){...}- As always, commit and push all your changes once you're done.
At the end of the exercise you will end up with aStudent.javaand aStudentTest.javaclass.
- Go ahead and try to add the rest of the
CRUDtests and functionality, write the methods forupdateGrade()anddeleteGrade()in theStudentclass.
Once you are done with the Student.java class.
-
Checkout to the
cohorts-featurebranch, there you will find aCohort.javaclass inside thesrc/main/javafolder, 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: -
Start by creating a new branch called:
cohorts-tests. -
Then create a
CohortTestclass and build a test for each of the following requirements:- A
Cohortinstance can add aStudentto theListof students. - A
Cohortinstance can get the currentListof students. - A
Cohortinstance can get the average, and it's being calculated correctly.
- A
-
Go back to the
StudentTestclass and refactor the code to avoid any repetition in the test cases, the use of the@Beforeannotation will be useful to achieve this, do the same with this newCohortTestclass if you find any code repetition as well.
- Follow the TDD cycle and create a new feature to find students by their ID like:
findStudentById(long id)in theCohortclass, remember to create the tests first before any production code.