Backend Handbook
Leapwise
  • 👋Introduction
  • Software Design Principles & Clean Code
    • Atomicity
    • Modularity
    • Hierarchy
    • Loose coupling
    • Asynchronous programming
  • Development Practices
    • JavaDocs
    • Technical Debt
    • Testing Guidelines
      • The Importance of Test Automation
      • The Testing Pyramid
        • Unit Tests
        • Integration Tests
        • End-to-End Tests
      • Mutation Testing
      • Contract Tests
        • REST Controller Contract testing
        • OpenAPI Contract testing
      • Testing Frameworks
        • JUnit 5
        • Testcontainers
        • Mockito
      • Writing Clean Tests - Best Practices
    • Common library
    • Generic CRUD
    • Update Facade
  • Development Tools & Environment
    • Monitoring
    • Performance tuning
    • Multi-tenancy & Configuration Management
    • Git practices
    • CI/CD
    • Maven
  • Project Management
    • Jira
    • Confluence documentation
    • SCRUM
    • Our ways of working
  • LIFE AT LEAPWISE
    • Introduction
    • Who are we?
    • What do we do?
    • Our values
    • Hiring process
      • Hiring: A Mid Frontend Developer's Point of View
    • Benefits we offer
    • Onboarding process
      • Onboarding: A Senior Digital Marketing Specialist's perspective
    • Mentorship program
    • Career development
      • Trainings & certificates we offer
      • Career development: A Senior Software Developer's Insight
    • Community building
    • Juniorship
    • First-hand info from our first team member
    • Join our team
Powered by GitBook
LogoLogo

Company

  • About
  • Culture
  • Services

Insights

  • Leapwise Newsletter
  • Blog

© Leapwise

On this page
  • About
  • Dependences
  • Rules and good practice
  • Test lifecycle

Was this helpful?

  1. Development Practices
  2. Testing Guidelines
  3. Testing Frameworks

JUnit 5

PreviousTesting FrameworksNextTestcontainers

Last updated 11 months ago

Was this helpful?

About

JUnit 5 is a Java testing framework that provides a platform for writing and running tests. It introduces modern features, extensibility, and improved architecture over its predecessors, facilitating robust and flexible test development in Java applications.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • Junit Platform → It provides a core foundation to help launching testing frameworks on JVM. It acts as an interface between JUnit and its clients such as build tools (Maven and Gradle) and IDE's (Eclipse and IntelliJ). It introduces the concept of a “Launcher” which external tools use to discover, filter, and execute tests.

  • Junit Jupiter → It provides a new programming model and extension model for writing tests and extensions in Junit 5. It has a whole new annotation to write test cases in Junit 5. It implements TestEngine API provided by Junit Platform so that Junit 5 test can be run.

  • Junit Vintage → This sub-project provides extensive support for writing test cases in JUnit 4 and JUnit 3. Thus, backward compatibility is been provided by this project.

Dependences

  • junit-jupiter-api

  • junit-platform-launcher

  • junit-vintage-engine

Rules and good practice

  • JUnit5 supports default (package), public and protected visibility, even if it is recommended to use the default (package) visibility, which improves the readability of code.

  • Test methods and lifecycle methods may be declared locally within the current test class, inherited from superclasses, or inherited from interfaces

  • Test methods and lifecycle methods must not be abstract

  • Ignores without any warning:

    • private classes and private methods

    • static methods

    • methods returning a value without being a TestFactory

Minimal requirements:

class TestClass {

    @Test
    void testMethod()
    {
        Assertions.assertEquals(1, 1);
    }
}

Test lifecycle

If not specified at the class level, the default behavior is the "per-method" test instance lifecycle. @TestInstance, a type-level annotation, configures the test instance lifecycle for the annotated class or interface. Setting the mode to PER_CLASS allows shared test instance state between methods, non-static @BeforeAll, and @AfterAll methods. @TestInstance can be used as a meta-annotation to create custom composed annotations inheriting its semantics.

JUnit5 Architecture
Test lifecycle
Page cover image