JUnit5 vs JUnit4

JUnit5 is a successor of the JUnit4 testing framework. JUnit5 introduces some notable new features:

  • Changes to annotations (JUnit4 -> JUnit5):
@BeforeClass -> @BeforeAll @Before -> @BeforeEach 

@AfterClass -> @AfterAll @After -> @AfterEach 

@Ignore -> @Disable

 

  • Tags for tagging and filtering via @Tag  annotation
  • Parameterized tests with @ParameterizedTest  annotation and a arguments source (@ValueSource , @CSVFileSource , @MethodSource , @EnumSource )
@ParameterizedTest 
@ValueSource(strings = { "Hello", "World" }) 
void test(String argument) {
      assertNotNull(argument); 
}

 

  • Repeated tests with @RepeatedTest  annotation
  • Dynamic tests – Tests generated via the factory method annotated with @TestFactory
@TestFactory 
Collection<DynamicTest> testFactory() {
    return Arrays.asList(
       dynamicTest("Test #1", () -> assertTrue(true)),
       dynamicTest("Test #2", () -> assertEquals(4, 2 * 2))
    ); 
}

 

Contextual Components

When using component-based architecture in the frontend, contextual components are wrapper components. They can act either as presentational or container components with an extended capability of passing a data up via yield functionality. One example, in Ember, would be a checkbox list:

// components/ui-checkbox-list.hbs (component template)
{{#each items as |item|}}
   {{yield item}}
{{/each}}

 

// Usage in other parts of the application

{{ui-checkbox-list items=_checkboxItems as |item|}}
{{ui-checkbox-item item=item}}
{{/ui-checkbox-list}}

 


“JUnits 4, 5 and Contextual Components” Tech Bite was brought to you by Kenan Klisura, Lead Software Engineer at Atlantbh. 

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

Decorator pattern
Software DevelopmentTech Bites
January 23, 2023

Decorator pattern

Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can customize to solve a particular design problem in your code. A decorator pattern allows users to add new functionality to an existing object without altering its structure. This design pattern comes under…

Leave a Reply