Versions Compared

Key

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

...

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

...

  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/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/maintest/javagroovy/liquibase/util/StringUtilTest.groovy

  3. The next step is to create the actual test

...

liquibase-core/src/main/java/liquibase/util/StringUtil.java

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 {
    private static final Pattern upperCasePattern = Pattern.compile(".*[A-Z].*");
    private static final Pattern lowerCasePattern = Pattern.compile(".*[a-z].*");
    private static final SecureRandom rnd = new SecureRandom();
    /**
     * Returns the trimmed (left and right) form of the input string. If the string is empty after trimming (or null
     * was passed in the first place), null is returned, i.e. the input string is reduced to nothing.
     * @param string the string to trim
     * @return the trimmed string or null
     */
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;
        }
    }
...
}

...

1. In the liquibase-core/src/src/test/groovy/liquibase/util/ directory, create or edit the StringUtilTest.groovy file . Add and add the following new test method to test the trimToNull() method in the Java class.

Info

The test method name should descriptive of what is being tested. At a minimum it should be the name of the method under test, but ideally it should describe what is being tested by the method.

...

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(or all the testss) you want to run.

Running via Maven

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

...