Class ClassAsserts


  • public class ClassAsserts
    extends java.lang.Object
    Utility class containing various method for making assertions on Class objects.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassAsserts()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertEqualsValid​(java.lang.Object o1, java.lang.Object o2, java.util.List excludedFields)
      Tests that a class' equals method notice changed fields.
      static void assertHasFactoryMethod​(java.lang.Class c)
      Tests the class has a static factory method getInstance()
      static <T> void assertPrivateConstructor​(java.lang.Class<T> c)
      Tests if there are any public constructors.
      static <T> T assertSingleton​(java.lang.Class<T> c)
      Tests that a class has a static factory method getInstance() and that it acts as a singleton.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • assertHasFactoryMethod

        public static void assertHasFactoryMethod​(java.lang.Class c)
        Tests the class has a static factory method getInstance()
        Parameters:
        c - the class to test
      • assertSingleton

        public static <T> T assertSingleton​(java.lang.Class<T> c)
        Tests that a class has a static factory method getInstance() and that it acts as a singleton. NB This method will create an instance of the class. It is your responsibility to clean up after yourself.
        Parameters:
        c - the class to test
        Returns:
        the singleton
      • assertPrivateConstructor

        public static <T> void assertPrivateConstructor​(java.lang.Class<T> c)
        Tests if there are any public constructors. Will fail on any public constructor, and simply return otherwise.
        Parameters:
        c - A class to test.
      • assertEqualsValid

        public static void assertEqualsValid​(java.lang.Object o1,
                                             java.lang.Object o2,
                                             java.util.List excludedFields)
                                      throws java.lang.IllegalAccessException
        Tests that a class' equals method notice changed fields. Also performs some testing of the hashCode method, but not as comprehensive.
        Parameters:
        o1 - An object to test on.
        o2 - Another object with all fields to be tested set to different values from o1
        excludedFields - A list of field names of fields that should not be included in the test.
        Throws:
        java.lang.IllegalAccessException