• TestingDocs
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

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com