TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    JUnit

    JUnit Test Execution Order Example

    JUnit Tutorial

    Introduction

    In this post, we will discuss the JUnit Test Execution Order Example. The test methods are simple calculator test cases. It is also not certain what order JUnit will run the test cases. The order may change on a different JVM or platform. By design, JUnit does not specify the execution order of test method invocations. However, relying on the JVM is not wise enough. As a matter of guidelines while writing unit tests, do not assume the order of execution.

     

     

    Junit_Test_ExecutionOrder1

     

    Sample example code:

    package com.testingdocs.junit.examples;
    
    import org.junit.Test;
    
    public class TestOrderExample
    {
        @Test
        public void addTest() {
            System.out.println("Check addition here");
        }
        
        @Test
        public void subTest() {
            System.out.println("Check Subtraction here");
        }
        
        @Test
        public void multiplyTest() {
            System.out.println("Check multiplication here");
        }
    
        @Test
        public void divideTest() {
            System.out.println("Check division here");
        }
    }

     

    Junit_Test_ExecutionOrder2

     

    @FixMethodOrder

    The default order of execution of JUnit tests within a class is deterministic but not predictable. Sometimes, if we need to specify the order of execution of test methods within a class we can use @FixMethodOrder.

    Sort the methods into a specified execution order.  Using MethodSorter.NAME_ASCENDING,  sorts the test methods by the method name, in lexicographic order.

     

    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class TestOrderExample
    {
        @Test
        public void addTest() {
            System.out.println("Check addition here");
        }
        
        @Test
        public void subTest() {
            System.out.println("Check Subtraction here");
        }
        
        @Test
        public void multiplyTest() {
            System.out.println("Check multiplication here");
        }
    
        @Test
        public void divideTest() {
            System.out.println("Check division here");
        }
    }

     

    Junit_Test_ExecutionOrder3

     

    Related Posts

    Getting Started with JUnit Framework

    JUnit /

    Getting Started with JUnit Framework

    Run JUnit tests from command line

    JUnit /

    Run JUnit tests from command line

    Working with JUnit in Eclipse IDE

    JUnit /

    Working with JUnit in Eclipse IDE

    Adding JUnit5 library to a Project

    JUnit /

    Adding JUnit5 library to a Project

    Test Failure JUnit

    JUnit /

    Debug JUnit Tests using IDE

    ‹ JUnit @Test Annotation Examples› JUnit @RunWith Annotation

    Recent Posts

    • Running Tests in Parallel with Selenium Grid
    • Advanced Selenium Features
    • Locating Web Elements
    • Running the Test Script
    • Writing Your First Selenium Test Script
    • Getting Started with Selenium Automation Testing
    • Setting Up the Environment
    • How can you monitor the Quality Assurance Audit?
    • Leveraging LambdaTest with Appium 2.0
    • Appium 2.0 Plugins ListAppium 2.0 Plugins
    • Touch Actions and Multi-Touch Actions
    • Changes in Drivers and Classes
    • Appium Inspector
    • Capabilities in Appium 2.0
    • Appium 2.0 Driver ListAppium 2.0 Driver Installation & Management
    CyberLink Multimedia Software

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com