Getting started with TDD

Books and a robot reading a book

1. Why?

2. Hands on 💻

For this hands on, you will need to create a project based on Gradle, since we need to add the dependency to JUnit 5 framework. You can do it by adding the library manually, if you want.

2.1. Recap the TDD

Test driven development cycle

2.2. Java

The steps to create a project and write a JUnit 5 are:

Options to create a new project on IntelliJ
Define the name of project as well as the artifact name
Project as is
Project dependencies

2.3. Domain

The domain will be a service that gets movies recommendations by user. Since Netflix is widely known, our service will be created using the name NetflixService and will be responsible to get recommendations given a user.

Feature: Recommendations

Scenario: Get movie recommendations by user
Given an user
When has recommendations
Then return a list of movies
Test failed
Test passed
Test failed
Mockito dependency
Test passed

2.4. Code

Below we can see the services. In the class RecommendationService we’re throwing an unchecked exception, since this method is not ready.

RecommendationService class
NetflixServiceTest class

3. Conclusion

Tests help us a lot daily and having automated tests much more. When possible use TDD or at least create the UT upfront, this will save time and money, since the scope is more accurate.

4. References



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luiz Gustavo De O. Costa

Luiz Gustavo De O. Costa

Hey friend!! I’m Luiz Gustavo, a Java developer and I’m here to learn and write about Java, tests and good practices