- 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
[email protected]:your-github-username/junit-tests.git
, whereyour-github-username
is 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 [email protected]:your-github-username/junit-tests.git
. - If you're using command line, then execute the following command line command:
git clone [email protected]: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:
id
should be along
number used to represent a "unique user" in our application.name
is aString
that holds the name of the student.grades
is anArrayList
that contains a list ofInteger
numbers.
-
Create a new branch called
students-tests
and read carefully the next instructions. -
Create a
StudentTest.java
class 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 theCRUD
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:
// 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 average of the students grades public double getGradeAverage();
- The
-
As always, commit and push all your changes once you're done.
At the end of the exercise you will ended up with aStudent.java
and aStudentTest.java
class.
- Go ahead and try to add the rest of the
CRUD
tests and functionality, write the methods forupdateGrade()
anddeleteGrade()
in theStudent
class.
Once you are done with the Student.java
class.
-
Checkout to the
cohorts-feature
branch, there you will find aCohort.java
class inside thesrc/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: -
Start by creating a new branch called:
cohorts-tests
. -
Then create a
CohortTest
class and build a test for each of the following requirements:- A
Cohort
instance can add aStudent
to theList
of students. - A
Cohort
instance can get the currentList
of students. - A
Cohort
instance can get the average and it's being calculated correctly.
- A
-
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 newCohortTest
class 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 theCohort
class, remember to create the tests first before any production code.