Migrating from JUnit 4 to JUnit 5: replacing rules with the extension model. Part 4

Luxoft Training
2 min readJan 23, 2020

--

Listing 8 The JUnit4CustomRuleTester class

In listing 8, we use the previously defined CustomRule by doing the following:

  1. We declare a public CustomRule field and we annotate it with @Rule (1).
  2. We create the myCustomRuleTest method and annotate it with @Test (2).

The result of the execution of this test is shown in figure 1. The effective execution of the test is surrounded by the additional messages provided into the evaluate method of the CustomStatement class.

Figure 1 The result of the execution of JUnit4CustomRuleTester.

We now turn our attention to the JUnit 5 approach. JUnit 5 allows similar effects as in the case of the JUnit 4 rules by introducing the own extensions. The code is shorter and it relies on the declarative annotations style. We first define the CustomExtension class, which is used as an argument of the @ExtendWith annotation on the tested class.

Read the rest of the article at — http://bit.ly/2tInYZz

Originally published at https://www.luxoft-training.com.

--

--

No responses yet