Giter Club home page Giter Club logo

junit-5_basics's Introduction

JUNIT-5_Basics ❤️

  • This module consists of Some Important and Basic Concepts of JUNIT-5 (Unit Testing) which is verymuch supportive for testing our Code with multiple test scripts in Eclipse/Spring framework.

Introduction 👋

  • This JUnit 5 tutorial talks about how it adapted java 8 style of coding and several other features as well.
  • JUnit 5 is most widely used testing framework for java applications.
  • For very long time, JUnit has been doing its job perfectly.
  • In between, JDK 8 brought very exciting features in java and most notably lambda expressions.
  • JUnit 5 aims to adapt java 8 style of coding and several other features as well, that’s why java 8 is required to create and execute tests in JUnit 5 (though it is possible to execute tests written with JUnit 3 or JUnit 4 for backward compatibility).

JUnit 5 Architecture 📺

 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit Platform

  • To be able to launch junit tests, IDEs, build tools or plugins need to include and extend platform APIs. It defines the TestEngine API for developing new testing frameworks that runs on the platform.
  • It also provides a Console Launcher to launch the platform from the command line and build plugins for Gradle and Maven.

JUnit Jupiter

  • It includes new programming and extension models for writing tests.
  • It has all new junit annotations and TestEngine implementation to run tests written with these annotations.

JUnit Vintage

  • It primary purpose is to support running JUnit 3 and JUnit 4 written tests on the JUnit 5 platform.
  • It’s there are backward compatibility.

Installation 📫

You can use JUnit 5 in your maven or gradle project by including minimum two dependencies i.e. Jupiter Engine Dependency and Platform Runner Dependency.

//pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
    <junit.jupiter.version>5.5.2</junit.jupiter.version>
    <junit.platform.version>1.5.2</junit.platform.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

//build.gradle
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2")
testRuntime("org.junit.platform:junit-platform-runner:1.5.2")

JUnit 5 Annotations 📌

@BeforeEach - The annotated method will be run before each test method in the test class.

@AfterEach - The annotated method will be run after each test method in the test class.

@BeforeAll - The annotated method will be run before all test methods in the test class. This method must be static.

@AfterAll - The annotated method will be run after all test methods in the test class. This method must be static.

@Test - It is used to mark a method as junit test

@DisplayName - Used to provide any custom display name for a test class or test method

@Disable - It is used to disable or ignore a test class or method from test suite.

@Nested - Used to create nested test classes

@Tag - Mark test methods or test classes with tags for test discovering and filtering

@TestFactory - Mark a method is a test factory for dynamic tests

Writing Tests in JUnit 5 😄

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import com.gowthamrajk.junit5.examples.Calculator;

public class AppTest {
 
@BeforeAll
static void setup(){
    System.out.println("@BeforeAll executed");
}
 
@BeforeEach
void setupThis(){
    System.out.println("@BeforeEach executed");
}
 
@Tag("DEV")
@Test
void testCalcOne() 
{
    System.out.println("======TEST ONE EXECUTED=======");
    Assertions.assertEquals( 4 , Calculator.add(2, 2));
}
 
@Tag("PROD")
@Disabled
@Test
void testCalcTwo() 
{
    System.out.println("======TEST TWO EXECUTED=======");
    Assertions.assertEquals( 6 , Calculator.add(2, 4));
}
 
@AfterEach
void tearThis(){
    System.out.println("@AfterEach executed");
}
 
@AfterAll
static void tear(){
    System.out.println("@AfterAll executed");
}
}

Test Suites ✔️

  • Using JUnit 5 test suites, you can run tests spread into multiple test classes and different packages.

  • JUnit 5 provides two annotations: @SelectPackages and @SelectClasses to create test suites.

  • To execute the suite, you will use @RunWith(JUnitPlatform.class).

    @RunWith(JUnitPlatform.class)
    @SelectPackages("com.gowthamrajk.junit5.examples")
    public class JUnit5TestSuiteExample 
    {
    
    }
    

Additionally, you can use following annotations for filtering test packages, classes or even test methods.

  1. @IncludePackages and @ExcludePackages to filter packages
  2. @IncludeClassNamePatterns and @ExcludeClassNamePatterns to filter test classes
  3. @IncludeTags and @ExcludeTags to filter test methods
@RunWith(JUnitPlatform.class)
@SelectPackages("com.gowthamrajkinjava.junit5.examples")
@IncludePackages("com.gowthamrajk.junit5.examples.packageC")
@ExcludeTags("PROD")
public class JUnit5TestSuiteExample 
{

}

Assertions ⌚

  • Assertions help in validating the expected output with actual output of a testcase.

  • To keep things simple, all JUnit Jupiter assertions are static methods in the org.junit.jupiter.Assertions class e.g. assertEquals(), assertNotEquals().

    void testCase() 
    {
        //Test will pass
        Assertions.assertNotEquals(3, Calculator.add(2, 2));
    
        //Test will fail 
        Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");
    
        //Test will fail 
        Supplier<String> messageSupplier  = ()-> "Calculator.add(2, 2) test failed";
        Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier);
      }
    

Assumptions ⌛

  • Assumptions class provides static methods to support conditional test execution based on assumptions.

  • A failed assumption results in a test being aborted. Assumptions are typically used whenever it does not make sense to continue execution of a given test method.

  • In test report, these test will be marked as passed.

  • JUnit jupiter Assumptions class has two such methods: assumeFalse(), assumeTrue().

    public class AppTest 
    {
        @Test
        void testOnDev() 
        {
            System.setProperty("ENV", "DEV");
            Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")), AppTest::message);
        }
    
        @Test
        void testOnProd() 
        {
            System.setProperty("ENV", "PROD");
            Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));  
        }
    
        private static String message () 
        {
            return "TEST Execution Failed :: ";
        }
    }
    

Backward Compatibility for JUnit 3 or JUnit 4 ⏩

  • JUnit 4 has been here for a quite long time and there are numerous tests written in junit 4.
  • JUnit Jupiter need to support those tests as well. For this purpose, JUnit Vintage sub-project is developed.
  • JUnit Vintage provides a TestEngine implementation for running JUnit 3 and JUnit 4 based tests on the JUnit 5 platform.



For more queries, reach me through [email protected] or whatsapp @ 9698382306


Show some ❤️ by starring this repository !!!


Organization Created & Maintained By

😄


Connect with me 👋:

code-Jamm.in GowthamRaj | YouTube GowthamRaj GowthamRaj  | Twitter GowthamRaj  | LinkedIn GowthamRaj  | Instagram


Copyright-and-license 📌

Code and documentation Copyright 2021 : Gowthamraj K

junit-5_basics's People

Contributors

gowthamrajk avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.