Welcome, dear student! In this repository, you will find 5 simple tasks designed to help you grasp the fundamentals of unit testing. These tasks are inspired by real-life scenarios extracted from larger projects.
The main objective of these tasks is to familiarize you with the workings of unit testing. Through completing them, you will gain insight into:
- Writing effective unit tests
- Creating meaningful and informative test names
- Debugging errors in tests
- Getting knowledge about code-coverage.
Below is a brief overview of the tasks:
- Task 1: Implement and correct unit tests for a basic calculator class.
- Task 2: Refctor existing tests in order to get them "green". Tests are written against the rules (wrong names, testing all methods in one test etc...)
- Task 3: Refactoring tests in order to remove redundant code using JUnit annotations - @BeforeEach, and @AfterEach.
- Task 4: Implement tests that check if tested class throws exception when given wrong input data.
- Task 5: Adding tests in order to get all tested class covered by unit tests.
You can do tasks in two ways. If you dont feel confident with git, follow first step. However, if you want to learn a little bit more - follow second step. Choice is yours :)
You can do it by executing mvn test -Dtest="NameOfTestClass"
command in console or simply clicking "play" buttons.
After completing each task, you can check if tests are "green". However, usually in production repository, some sort of integration workflow will be implemented. For example, we can add extra verification by GIT if some bugfix or commit is pushed into the branch - better not trust "Mr. Smith" that every test passes succesfully. Automatic code verification is more trustworthy. If your code passes that verification it can be automaticlly merged into main branch (after approval).
To get started, fork this repository to your local machine and navigate to the project directory.
Create a new branch when working on each task (e.g git checkout -b task_1_fix
, git checkout -b task_2_fix
...). - you can do it in the project folder via IDE terminal.When you think that task is finished, do commands below:
Now, you can check if your solution is correct by making pull request to the main repository - git add .
, then git commit -am "solution"
and git push --set-upstream origin "name_of_your_branch"
.
In console you will see link to create pull request
- click on it.
When pull request
is created, and accepted by me (repository owner) GitHub will show you if all tests pass ("are green") .
If your solution is correct, it will be seen as valid for merging into main branch.
Note that below informations are showed only to the repository owner. However, I can share screen and you can see if your solution is correct :)
Feel free to reach out if you have any questions.
Using IntelliJ is recommended, but you can use any IDE you want
The tests for each task are provided in the tests
folder within each task's directory. To run the tests, follow these steps:
- Navigate to the task folder (e.g.,
task1
,task2
, etc.) - Open a terminal or command prompt in that folder
- Run the command to execute the tests (e.g.,
mvn test -Dtest="NameOfTestClass"
)
If you encounter any difficulties or have questions, do not hesitate to ask! Feel free to create an issue in this repository, and I'll be happy to assist you.
Good luck with the tasks, and happy testing! 🚀