Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JUnit 5 Deep Dive Using Test-Driven Development [Video]

DZone 's Guide to

JUnit 5 Deep Dive Using Test-Driven Development [Video]

Learn how to use JUnit 5 for unit testing with the test-driven development approach.

· Performance Zone ·
Free Resource

Please find below a video of a deep dive session on JUnit 5. In this session, I have described the new features of JUnit 5 and how can it help in writing code using Test-Driven Development (TDD) methodology.

Prerequisites

  • You should have Spring Tools Suite (STS) 4 or Eclipse IDE installed on your laptop with JDK 8 or above installed.
  • Basic understanding of the philosophy behind unit tests.

Note: For maximum benefit from this session, you can try out the steps together with this video.


Below are the key steps involved in Test-Driven Development.

First, the developer writes the unit tests to cover the new functionality under development. Obviously, these tests would fail to start with but still, the developer should run these unit tests to ensure it fails as expected (not due to some other reasons).

Then he implements the code to make the unit tests pass. The developer might need to go through multiple iterations of writing and running the tests until all the tests are green.

Once the implementation for the desired functionality is complete (signaled by all the tests running successfully). The developer can refactor the code to ensure it is clean to easily support further development on top of it. After refactoring, the developer re-runs the unit tests to ensure all still all tests are passing.

The developer may also refactor the unit test code in case needed. But as a best practice, he should deliberately make it purposefully fail to ensure no false positives are introduced as part of this exercise. 

The entire cycle of steps mentioned above can be repeated for each new functionality to be implemented.

Following are some of the key benefits of this test-driven development.

  • This approach makes sure it is NOT possible for developers to write untestable code.

  • The unit tests provide the much-needed support for refactoring, which is very important to ensure the source code always remains clean and new requirements or changes to the existing functionality can be delivered smoothly forever.

Topics:
junit ,tdd ,unit testing ,tutorial ,sts ,JAVA ,eclipse ,code coverage

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}