Class TestUtil

java.lang.Object
com.reallifedeveloper.tools.test.TestUtil

public final class TestUtil extends Object
Miscellaneous utility methods that are useful when testing.
Author:
RealLifeDeveloper
  • Field Details

  • Method Details

    • findFreePort

      public static int findFreePort() throws IOException
      Gives a port number on the local machine that no server process is listening to.
      Returns:
      a free port number
      Throws:
      IOException - if an I/O error occurs when trying to open a socket
    • parseDate

      public static Date parseDate(String date)
      Parses a date string on the form "yyyy-MM-dd" and returns the corresponding java.util.Date object.
      Parameters:
      date - the date string to parse, should be on the form "yyyy-MM-dd"
      Returns:
      the java.util.Date corresponding to date
      Throws:
      IllegalArgumentException - if date cannot be parsed
    • parseDateTime

      public static Date parseDateTime(String dateTime)
      Parses a date and time string on the form "yyyy-MM-dd HH:mm:ss" and returns the corresonding java.util.Date object.
      Parameters:
      dateTime - the date+time string to parse, should be on the form "yyyy-MM-dd HH:mm:ss"
      Returns:
      the java.util.Date corresponding to dateTime
      Throws:
      IllegalArgumentException - if dateTime cannot be parsed
    • writeToFile

      public static void writeToFile(String s, String filename, Charset charset) throws IOException
      Writes a string to a file using the given character encoding.
      Parameters:
      s - the string to write
      filename - the name of the file to write to
      charset - the character set to use, e.g., java.nio.charset.StandardCharsets.UTF_8
      Throws:
      IOException - if writing to the file failed
    • readResource

      public static String readResource(String resourceName) throws IOException
      Reads a string from a classpath resource, which is assumed to be UTF-8 encoded text.
      Parameters:
      resourceName - the name of the classpath resource to read
      Returns:
      a string representation of the classpath resource resourceName
      Throws:
      IOException - if reading the resource failed
    • injectField

      public static void injectField(Object obj, String fieldName, Object value)
      Injects a value into an object's field, which may be private.
      Parameters:
      obj - the object in which to inject the value
      fieldName - the name of the field
      value - the value to inject
      Throws:
      IllegalArgumentException - if obj or fieldName is null
      IllegalStateException - if reflecction failure
    • getFieldValue

      public static @Nullable Object getFieldValue(Object obj, String fieldName)
      Gives the value of an object's field, which may be private.
      Parameters:
      obj - the object containing the field
      fieldName - the name of the field
      Returns:
      the value of the field fieldName in the object obj
      Throws:
      IllegalArgumentException - if obj or fieldName is null
      IllegalStateException - if reflection failure