Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Liquibase code contains both unit tests and integration tests. This document explains the basics of creating Unit tests that will be used by the automated build process to validate Liquibase and reduce regressions.

What are Unit Tests?

Unit tests test validate functionality in isolation, without any external system interaction. Because there are no external dependencies, these tests need no additional setup and run fast.

Liquibase Unit tests are written using the Spock testing framework.

Naming and Location of the Unit Test

Unit tests are written at the Java class level. There is a 1-1 mapping between the unit test class and the Java class being tested. The unit test class has the same package and name as the Java class, but with Test.groovy appended. All test classes are stored using the src/test/groovy structure expected by Maven.

...

  1. Replace main/java in the file path with the new substring src/test/groovy

    • liquibase-core/src/main/java/liquibase/util/

    • liquibase-core/src/test/groovy/liquibase/util/

  2. If a unit test doesn’t already exist, create a new unit test class with the same base filename as the Java class and append Test.groovy

    1. liquibase-core/src/test/groovy/liquibase/util/StringUtilTest.groovy

  3. The next step is to create the actual test

Creating the Unit Test

In this example, we added a new method in this class named public static String StringUtil.trimToNull(String). The goal of the method is to return a new string with leading and training whitespace removed. If the resulting string is empty, then return null.

The Java Class to Test

See the actual Java class in GitHub -https://github.com/liquibase/liquibase/blob/master/liquibase-core/src/main/java/liquibase/util/StringUtil.java- or the edited example below:

...

Code Block
languagegroovy
package liquibase.util;

import liquibase.ExtensibleObject;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Pattern;

/**
 * Various utility methods for working with strings.
 */
public class StringUtil {
... 
    public static String trimToNull(String string) {
        if (string == null) {
            return null;
        }
        String returnString = string.trim();
        if (returnString.isEmpty()) {
            return null;
        } else {
            return returnString;
        }
    }
...
}

The Unit Test

See the actual Unit Test class in GitHub -https://github.com/liquibase/liquibase/blob/master/liquibase-core/src/test/groovy/liquibase/util/StringUtilTest.groovy- or the edited example below:

...

Code Block
@Unroll("#featureName: '#input'")
def "trimToNull"() {
    expect:
    StringUtil.trimToNull(input) == expected

    where:
    input                     | expected
    "test string"             | "test string"
    "test string   "          | "test string"
    "   test string"          | "test string"
    "   test string     "     | "test string"
    "test    string"          | "test    string"
    ""                        | null
    "    "                    | null
    "\n\r\ttest string\r\n\t" | "test string"
}

Running and Testing the Unit Test

Running via IDE

The Spock framework is an extension of JUnit. During development it is normally executed through your IDE just like any other test. Your IDE will let you choose which test(s) you want to run.

Running via Maven

You are also able to run all unit tests via maven.

...