Test Driven Development Using JUnit5. Part-3(Dissecting The Annotations)

In this section, we are going to go a little deep into the various annotations present in the JUnit5 testing framework, these annotations are the elements we use to carry out tests.

@Test

You have seen this already in the last article, it’s usually applied on a method, it’s kind of like a marker that marks the method that it is being applied to as a test method,it’s basically declaring that ‘this method over here is a test method’ it’s usually being applied like this:

 

@RepeatedTest

In JUnit5 there is a concept of repeated tests i.e a test that you want to run more than once automatically.  The @RepeatedTest can take attributes such as value which is the number of times you want that particular test to run, name the name given to each repetition.  When applying it, it looks like this:

The {currentRepetition} and {totalRepetitions} used above are JUnit5 specific elements that are used to get the current repetition number being executed and total number of repetitions to be executed respectively.

 

@DisplayName

This attribute is used to show a custom display name for each test.  Using the code above, to show a custom display name you do something like this:

@BeforeEach

This is also an attribute in JUnit5, it tells the JUnit Test Engine that this method must be executed before each test case in the same class is executed, still with the code above, when you have something like this:

The iComeFirst() method will always execute before each repetition of the testAddition method.

 

Play with these for a while, in the next article we will continue dissecting JUnit5 annotations.  You can find the complete codebase here at github,

 

2 comments on “Test Driven Development Using JUnit5. Part-3(Dissecting The Annotations)

Leave a Reply

Your email address will not be published. Required fields are marked *