Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

JUnit test execution order

farenda 2016-01-06 0

How to control JUnit test execution order? It’s not only possible, but also easy in recent versions of the most popular testing library!

For a long time JUnit didn’t have any means to control tests execution order and all tests were executed in non predictable order. This is still default behavior, but JUnit 4.11 shipped with @FixMethodOrder annotation that allow to customize that. The annotation takes org.junit.runners.MethodSorters as its only argument and can change execution order as follows:

  • DEFAULT: sorts tests in non predictable order
  • JVM: leaves the ordering for the JVM, which can differ from run to run
  • NAME_ASCENDING: sorts tests by method name in lexicographical order.

In the following example we’re going to tell JUnit to execute tests in order dictated by tests’ names:

package com.farenda.junit;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static org.junit.Assert.assertEquals;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ExecutionOrderTest {

    @Test
    public void minNumber() {
        System.out.println("Executing first test.");
        assertEquals(24, Math.min(24, 42));
    }

    @Test
    public void maxNumber() {
        System.out.println("Executing second test.");
        assertEquals(42, Math.max(24, 42));
    }
}

In the output you can see that test ordering works:

Executing second test.
Executing first test.

Simple as that. :-)

How pros do it

They don’t shouldn’t, because tests should be independent.
Anyway, fixing methods order may be helpful to force some execution order to recreate a bug that occurred, when they were executed in a different order.

Let’s see how Spring Framework is using FixMethodOrder to force order of tests that operate on database – DataSourceOnlySqlScriptsTests.java:

package org.springframework.test.context.jdbc;

import javax.sql.DataSource;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;

// imports cut for brevity

import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;

/**
 * Integration tests for {@link Sql @Sql} support with only a {@link DataSource}
 * present in the context (i.e., no transaction manager).
 *
 * @author Sam Brannen
 * @since 4.1
 */
@RunWith(SpringJUnit4ClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration
@Sql({ "schema.sql", "data.sql" })
@DirtiesContext
public class DataSourceOnlySqlScriptsTests {

        private JdbcTemplate jdbcTemplate;

        @Autowired
        public void setDataSource(DataSource dataSource) {
                this.jdbcTemplate = new JdbcTemplate(dataSource);
        }

        @Test
        // test##_ prefix is required for @FixMethodOrder.
        public void test01_classLevelScripts() {
                assertInTransaction(false);
                assertNumUsers(1);
        }

        @Test
        @Sql({ "drop-schema.sql", "schema.sql", "data.sql", "data-add-dogbert.sql" })
        // test##_ prefix is required for @FixMethodOrder.
        public void test02_methodLevelScripts() {
                assertInTransaction(false);
                assertNumUsers(2);
        }

        protected void assertNumUsers(int expected) {
                assertEquals("Number of rows in the 'user' table.", expected,
                        JdbcTestUtils.countRowsInTable(jdbcTemplate, "user"));
        }

        @Configuration
        static class Config {

                @Bean
                public DataSource dataSource() {
                        return new EmbeddedDatabaseBuilder()//
                        .setName("empty-sql-scripts-without-tx-mgr-test-db")//
                        .build();
                }
        }

}
Share with the World!
Categories JUnit Tags java, junit, unit-tests
Previous: JUnit Assume – execute tests conditionally
Next: JUnit Rules – code around tests

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok