Class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>>

java.lang.Object
org.assertj.core.api.AbstractAssert<SELF,Class<?>>
org.assertj.core.api.AbstractClassAssert<SELF>
Type Parameters:
SELF - the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
All Implemented Interfaces:
Assert<SELF,Class<?>>, Descriptable<SELF>, ExtensionPoints<SELF,Class<?>>
Direct Known Subclasses:
ClassAssert

public abstract class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>> extends AbstractAssert<SELF,Class<?>>
Base class for all implementations of assertions for Classes.
Author:
William Delanoue, Mikhail Mazursky
  • Constructor Details

    • AbstractClassAssert

      protected AbstractClassAssert(Class<?> actual, Class<?> selfType)
  • Method Details

    • isAssignableFrom

      public SELF isAssignableFrom(Class<?>... others)
      Verifies that the actual Class is assignable from others Class

      Example:

       class Jedi {}
       class HumanJedi extends Jedi {}
      
       // this assertion succeeds:
       assertThat(Jedi.class).isAssignableFrom(HumanJedi.class);
      
       // this assertion fails:
       assertThat(HumanJedi.class).isAssignableFrom(Jedi.class);
      Parameters:
      others - Class who can be assignable from.
      Returns:
      this assertions object
      Throws:
      AssertionError - if the actual Class is null.
      IllegalArgumentException - if no others classes have been specified.
      AssertionError - if the actual Class is not assignable from all of the others classes.
      See Also:
    • isNotInterface

      public SELF isNotInterface()
      Verifies that the actual Class is not an interface.

      Example:

       interface Jedi {}
       class HumanJedi implements Jedi {}
      
       // this assertion succeeds:
       assertThat(HumanJedi.class).isNotInterface();
      
       // this assertion fails:
       assertThat(Jedi.class).isNotInterface();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not an interface.
    • isInterface

      public SELF isInterface()
      Verifies that the actual Class is an interface.

      Example:

       interface Jedi {}
       class HumanJedi implements Jedi {}
      
       // this assertion succeeds:
       assertThat(Jedi.class).isInterface();
      
       // this assertion fails:
       assertThat(HumanJedi.class).isInterface();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not an interface.
    • isAbstract

      public SELF isAbstract()
      Verifies that the actual Class is abstract (has abstract modifier).

      Example:

       public abstract class MyClass { }
      
       // this assertion succeeds:
       assertThat(MyClass.class).isAbstract();
      
       // this assertion fails:
       assertThat(String.class).isAbstract();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not abstract.
      Since:
      3.12.0
    • isAnnotation

      public SELF isAnnotation()
      Verifies that the actual Class is an annotation.

      Example:

       public @interface Jedi {}
      
       // these assertions succeed:
       assertThat(Jedi.class).isAnnotation();
       assertThat(Override.class).isAnnotation();
       assertThat(Deprecated.class).isAnnotation();
      
       // this assertion fails:
       assertThat(String.class).isAnnotation();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not an annotation.
    • isNotAnnotation

      public SELF isNotAnnotation()
      Verifies that the actual Class is not an annotation.

      Example:

       public @interface Jedi {}
      
       // this assertion succeeds:
       assertThat(String.class).isNotAnnotation();
      
       // these assertions fail:
       assertThat(Jedi.class).isNotAnnotation();
       assertThat(Override.class).isNotAnnotation();
       assertThat(Deprecated.class).isNotAnnotation();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is an annotation.
    • isFinal

      public SELF isFinal()
      Verifies that the actual Class is final (has final modifier).

      Example:

       // these assertions succeed:
       assertThat(String.class).isFinal();
       assertThat(Math.class).isFinal();
      
       // these assertions fail:
       assertThat(Object.class).isFinal();
       assertThat(Throwable.class).isFinal();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not final.
    • isNotFinal

      public SELF isNotFinal()
      Verifies that the actual Class is not final (does not have final modifier).

      Example:

       // these assertions succeed:
       assertThat(Object.class).isNotFinal();
       assertThat(Throwable.class).isNotFinal();
      
       // these assertions fail:
       assertThat(String.class).isNotFinal();
       assertThat(Math.class).isNotFinal();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is final.
    • isPublic

      public SELF isPublic()
      Verifies that the actual Class is public (has public modifier).

      Example:

       protected class MyClass { }
      
       // these assertions succeed:
       assertThat(String.class).isPublic();
       assertThat(Math.class).isPublic();
      
       // This assertion fails:
       assertThat(MyClass.class).isPublic();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not public.
      Since:
      2.7.0 / 3.7.0
    • isProtected

      public SELF isProtected()
      Verifies that the actual Class is protected (has protected modifier).

      Example:

       public class MyClass { }
      
       // this assertion succeeds:
       assertThat(MyClass.class).isProtected();
      
       // these assertions fail:
       assertThat(String.class).isProtected();
       assertThat(Math.class).isProtected();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not protected.
      Since:
      2.7.0 / 3.7.0
    • isPackagePrivate

      public SELF isPackagePrivate()
      Verifies that the actual Class is package-private (has no modifier).

      Example:

       class MyClass { }
      
       // this assertion succeeds:
       assertThat(MyClass.class).isPackagePrivate();
      
       // these assertions fail:
       assertThat(String.class).isPackagePrivate();
       assertThat(Math.class).isPackagePrivate();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not package-private.
      Since:
      3.15.0
    • isStatic

      public SELF isStatic()
      Verifies that the actual Class is static (has static modifier).

      Example:

       class OuterClass {
          static class StaticNestedClass { }
       }
       // this assertion succeeds:
       assertThat(OuterClass.StaticNestedClass.class).isStatic();
      
       // these assertions fail:
       assertThat(Object.class).isStatic();
       assertThat(Throwable.class).isStatic();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is not static.
      Since:
      3.23.0
    • isNotStatic

      public SELF isNotStatic()
      Verifies that the actual Class is not static (does not have static modifier).

      Example:

       // these assertions succeed:
       assertThat(Object.class).isNotStatic();
       assertThat(Throwable.class).isNotStatic();
      
       class OuterClass {
          static class StaticNestedClass { }
       }
       // this assertion fails:
       assertThat(OuterClass.StaticNestedClass.class).isNotStatic();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class is static.
      Since:
      3.23.0
    • hasAnnotations

      @SafeVarargs public final SELF hasAnnotations(Class<? extends Annotation>... annotations)
      Verifies that the actual Class has the given Annotations.

      Example:

       @Target(ElementType.TYPE)
       @Retention(RetentionPolicy.RUNTIME)
       private static @interface Force { }
      
       @Target(ElementType.TYPE)
       @Retention(RetentionPolicy.RUNTIME)
       private static @interface Hero { }
      
       @Target(ElementType.TYPE)
       @Retention(RetentionPolicy.RUNTIME)
       private static @interface DarkSide { }
      
       @Hero @Force
       class Jedi implements Jedi {}
      
       // this assertion succeeds:
       assertThat(Jedi.class).containsAnnotations(Force.class, Hero.class);
      
       // this assertion fails:
       assertThat(Jedi.class).containsAnnotations(Force.class, DarkSide.class);
      Parameters:
      annotations - annotations who must be attached to the class
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contains all of these annotations.
    • hasAnnotationsForProxy

      protected SELF hasAnnotationsForProxy(Class<? extends Annotation>[] annotations)
    • hasAnnotation

      public SELF hasAnnotation(Class<? extends Annotation> annotation)
      Verifies that the actual Class has the given Annotation.

      Example:

       @Target(ElementType.TYPE)
       @Retention(RetentionPolicy.RUNTIME)
       private static @interface Force { }
       @Force
       class Jedi implements Jedi {}
      
       // this assertion succeeds:
       assertThat(Jedi.class).containsAnnotation(Force.class);
      
       // this assertion fails:
       assertThat(Jedi.class).containsAnnotation(DarkSide.class);
      Parameters:
      annotation - annotations who must be attached to the class
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contains all of these annotations.
    • hasSuperclass

      public SELF hasSuperclass(Class<?> superclass)
      Verifies that the actual Class has the given class as direct superclass (as in Class.getSuperclass()).

      The superclass should always be not null, use hasNoSuperclass() to verify the absence of the superclass.

      Example:

       // this assertion succeeds:
       assertThat(Integer.class).hasSuperclass(Number.class);
      
       // this assertion succeeds as superclass for array classes is Object:
       assertThat(Integer[].class).hasSuperclass(Object.class);
      
       // this assertion fails:
       assertThat(String.class).hasSuperclass(Number.class);
      
       // this assertion fails as only direct superclass matches:
       assertThat(String.class).hasSuperclass(Object.class);
      
       // this assertion fails as interfaces are not superclasses:
       assertThat(String.class).hasSuperclass(Comparable.class);
      Parameters:
      superclass - the class which must be the direct superclass of actual.
      Returns:
      this assertions object
      Throws:
      NullPointerException - if superclass is null.
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't have the given class as direct superclass.
      Since:
      3.15.0
      See Also:
    • hasNoSuperclass

      public SELF hasNoSuperclass()
      Verifies that the actual Class has no superclass (as in Class.getSuperclass(), when null is returned).

      Example:

       // this assertion succeeds as Object has no superclass:
       assertThat(Object.class).hasNoSuperclass();
      
       // this assertion succeeds as interfaces have no superclass:
       assertThat(Cloneable.class).hasNoSuperclass();
      
       // this assertion succeeds as primitive types have no superclass:
       assertThat(Integer.TYPE).hasNoSuperclass();
      
       // this assertion succeeds as void type has no superclass:
       assertThat(Void.TYPE).hasNoSuperclass();
      
       // this assertion fails as Integer has Number as superclass:
       assertThat(Integer.class).hasNoSuperclass();
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class has a superclass.
      Since:
      3.15.0
      See Also:
    • hasFields

      @Deprecated public SELF hasFields(String... fields)
      Deprecated.
      Parameters:
      fields - the fields who must be in the class.
      Returns:
      this assertions object
    • hasPublicFields

      public SELF hasPublicFields(String... fields)
      Verifies that the actual Class has the given accessible public fields (as in Class.getFields()).

      Example:

       class MyClass {
           public String fieldOne;
           protected String fieldTwo;
           String fieldThree;
           private String fieldFour;
       }
      
       // this assertion succeeds:
       assertThat(MyClass.class).hasPublicFields("fieldOne");
      
       // these assertions fail:
       assertThat(MyClass.class).hasPublicFields("fieldTwo");
       assertThat(MyClass.class).hasPublicFields("fieldThree");
       assertThat(MyClass.class).hasPublicFields("fieldFour");
       assertThat(MyClass.class).hasPublicFields("unknownField");

      The assertion succeeds if no given fields are passed and the actual Class has no accessible public fields.

      Parameters:
      fields - the fields who must be in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contain all of the fields.
      See Also:
    • hasOnlyPublicFields

      public SELF hasOnlyPublicFields(String... fields)
      Verifies that the actual Class only has the given accessible public fields (as in Class.getFields()) and nothing more in any order.

      Example:

       class MyClass {
           public String fieldOne;
           public String fieldTwo;
           private String fieldThree;
       }
      
       // these assertions succeed:
       assertThat(MyClass.class).hasOnlyPublicFields("fieldOne", "fieldTwo");
       assertThat(MyClass.class).hasOnlyPublicFields("fieldTwo", "fieldOne");
      
       // this assertion fails:
       assertThat(MyClass.class).hasOnlyPublicFields("fieldOne");

      The assertion succeeds if no given fields are passed and the actual Class has no accessible public fields.

      Parameters:
      fields - all the fields that are expected to be in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if fields are not all the actual Class's accessible public fields.
      Since:
      2.7.0 / 3.7.0
      See Also:
    • hasDeclaredFields

      public SELF hasDeclaredFields(String... fields)
      Verifies that the actual Class has the given declared fields (as in Class.getDeclaredFields()).

      Example:

       class MyClass {
           public String fieldOne;
           private String fieldTwo;
       }
      
       // this assertion succeeds:
       assertThat(MyClass.class).hasDeclaredFields("fieldOne", "fieldTwo");
      
       // this assertion fails:
       assertThat(MyClass.class).hasDeclaredFields("fieldThree");

      The assertion succeeds if no given fields are passed and the actual Class has no declared fields.

      Parameters:
      fields - the fields who must be declared in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contains all of the field.
      See Also:
    • hasOnlyDeclaredFields

      public SELF hasOnlyDeclaredFields(String... fields)
      Verifies that the actual Class only has the given declared fields and nothing more in any order (as in Class.getDeclaredFields()).

      Example:

       class MyClass {
           public String fieldOne;
           public String fieldTwo;
           private String fieldThree;
           private String fieldFour;
       }
      
       // this assertion succeeds:
       assertThat(MyClass.class).hasOnlyDeclaredFields("fieldOne", "fieldTwo", "fieldThree", "fieldFour");
      
       // this assertion fails:
       assertThat(MyClass.class).hasOnlyDeclaredFields("fieldOne", "fieldThree");

      The assertion succeeds if no given fields are passed and the actual Class has no declared fields.

      Parameters:
      fields - all the fields that are expected to be in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if fields are not all the declared fields of the actual Class.
      Since:
      2.7.0 / 3.7.0
      See Also:
    • hasMethods

      public SELF hasMethods(String... methodNames)
      Verifies that the actual Class has the given methods (including inherited) whatever their visibility are.

      Example:

       class MySuperClass {
           public void superMethod() {}
           private void privateSuperMethod() {}
       }
      
       class MyClass extends MySuperClass {
           public void methodOne() {}
           private void methodTwo() {}
       }
      
       // this assertion succeeds:
       assertThat(MyClass.class).hasMethods("methodOne", "methodTwo", "superMethod", "privateSuperMethod");
      
       // this assertion fails:
       assertThat(MyClass.class).hasMethods("methodThree");
      Parameters:
      methodNames - the method names which must be in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contains all of the method names.
      Since:
      2.7.0 / 3.7.0
    • hasDeclaredMethods

      public SELF hasDeclaredMethods(String... methodNames)
      Verifies that the actual Class has the given declared methods.

      Example:

       class MySuperClass {
           public void superMethod() {}
       }
      
       class MyClass extends MySuperClass {
           public void methodOne() {}
           private void methodTwo() {}
       }
      
       // This assertion succeeds:
       assertThat(MyClass.class).hasDeclaredMethods("methodOne", "methodTwo");
      
       // these assertions fail:
       assertThat(MyClass.class).hasDeclaredMethods("superMethod");
       assertThat(MyClass.class).hasDeclaredMethods("methodThree");

      The assertion succeeds if no given methods are passed and the actual Class has no declared methods.

      Parameters:
      methodNames - the method names which must be declared in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contains all of the given methods.
      Since:
      2.7.0 / 3.7.0
    • hasPublicMethods

      public SELF hasPublicMethods(String... methodNames)
      Verifies that the actual Class has the given public methods.

      Example:

       class MyClass {
           public void methodOne() {}
           public void methodTwo() {}
           protected void methodThree() {}
       }
      
       // these assertions succeed:
       assertThat(MyClass.class).hasPublicMethods("methodOne");
       assertThat(MyClass.class).hasPublicMethods("methodOne", "methodTwo");
      
       // these assertions fail:
       assertThat(MyClass.class).hasPublicMethods("methodOne", "methodThree");
       assertThat(MyClass.class).hasPublicMethods("methodThree");
      Parameters:
      methodNames - the public method names which must be in the class.
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class doesn't contains all of the given public methods.
      Since:
      2.7.0 / 3.7.0
    • hasPackage

      public SELF hasPackage(String packageName)
      Verifies that the actual Class has the given package name (as in Class.getPackage()).

      Example:

       package one.two;
      
       class MyClass {}
      
       // this assertions succeeds:
       assertThat(MyClass.class).hasPackage("one.two");
      
       // these assertions fail:
       assertThat(MyClass.class).hasPackage("one");
       assertThat(MyClass.class).hasPackage("");
       assertThat(MyClass.class).hasPackage("java.lang");
      Parameters:
      packageName - the package name the class should have
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class does not have the given package.
      Since:
      3.18.0
    • hasPackage

      public SELF hasPackage(Package aPackage)
      Verifies that the actual Class has the given package (as in Class.getPackage()).

      Example:

       package one.two;
      
       class MyClass {}
      
       // these assertions succeed:
       assertThat(MyClass.class).hasPackage(Package.getPackage("one.two"));
       assertThat(MyClass.class).hasPackage(MyClass.class.getPackage());
      
       // these assertions fail:
       assertThat(MyClass.class).hasPackage(Package.getPackage("one"));
       assertThat(MyClass.class).hasPackage(Package.getPackage(""));
       assertThat(MyClass.class).hasPackage(Object.class.getPackage());
      Parameters:
      aPackage - the package the class should have
      Returns:
      this assertions object
      Throws:
      AssertionError - if actual is null.
      AssertionError - if the actual Class does not have the given package.
      Since:
      3.18.0