Top Description Fields Constructors Methods
org.assertj.core.api

public abstract Class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>>

extends AbstractAssert<SELF extends AbstractClassAssert<SELF>, Class<?>>
Class Inheritance
Known Direct Subclasses
org.assertj.core.api.ClassAssert
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.
Static Imports
java.util.Objects.requireNonNull, org.assertj.core.error.ClassModifierShouldBe.shouldBeFinal, .ClassModifierShouldBe.shouldBePackagePrivate, .ClassModifierShouldBe.shouldBePrivate, .ClassModifierShouldBe.shouldBeProtected, .ClassModifierShouldBe.shouldBePublic, .ClassModifierShouldBe.shouldBeStatic, .ClassModifierShouldBe.shouldNotBeFinal, .ClassModifierShouldBe.shouldNotBeStatic, .ShouldBeAbstract.shouldBeAbstract, .ShouldBeAnnotation.shouldBeAnnotation, .ShouldBeAnnotation.shouldNotBeAnnotation, .ShouldBeAssignableTo.shouldBeAssignableTo, .ShouldBeInterface.shouldBeInterface, .ShouldBeInterface.shouldNotBeInterface, .ShouldBePrimitive.shouldBePrimitive, .ShouldBeRecord.shouldBeRecord, .ShouldBeRecord.shouldNotBeRecord, .ShouldBeSealed.shouldBeSealed, .ShouldBeSealed.shouldNotBeSealed, .ShouldHaveNoPackage.shouldHaveNoPackage, .ShouldHaveNoSuperclass.shouldHaveNoSuperclass, .ShouldHavePackage.shouldHavePackage, .ShouldHavePermittedSubclasses.shouldHavePermittedSubclasses, .ShouldHaveRecordComponents.shouldHaveRecordComponents, .ShouldHaveSuperclass.shouldHaveSuperclass, .ShouldNotBeNull.shouldNotBeNull, .ShouldNotBePrimitive.shouldNotBePrimitive, org.assertj.core.util.Arrays.array, .Sets.newLinkedHashSet

Base class for all implementations of assertions for Classes.
Authors
William Delanoue, Mikhail Mazursky

Field Summary

Modifier and TypeField and Description
pack-priv Classes
Inherited from org.assertj.core.api.AbstractAssert:
actualassertionErrorCreatorconditionscustomRepresentationinfomyselfobjectsprintAssertionsDescriptionthrowUnsupportedExceptionOnEquals

Constructor Summary

AccessConstructor and Description
protected
AbstractClassAssert(Class<?> actual, Class<?> selfType)

Method Summary

Modifier and TypeMethod and Description
private void
private void
private void
assertHasPackage(String packageName)

private void
private void
assertHasPermittedSubclasses(Class<?>[] expectedPermittedSubclasses)

private void
private void
assertHasSuperclass(Class<?> superclass)

private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private void
private static Class<?>[]
private static Set<String>
public SELF

Returns:

this assertions object
hasAnnotation
(Class<? extends Annotation>
annotations who must be attached to the class
annotation
)

Verifies that the actual Class has the given Annotation.

public final SELF

Returns:

this assertions object
hasAnnotations
(Class<? extends Annotation>...
annotations who must be attached to the class
annotations
)

Verifies that the actual Class has the given Annotations.

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

public SELF

Returns:

this assertions object
hasDeclaredFields
(String...
the fields who must be declared in the class.
fields
)

Verifies that the actual Class has the given declared fields (as in Class#getDeclaredFields()).

public SELF

Returns:

this assertions object
hasDeclaredMethods
(String...
the method names which must be declared in the class.
methodNames
)

Verifies that the actual Class has the given declared methods.

public SELF

Returns:

this assertions object
hasFields
(String...
the fields who must be in the class.
fields
)

Deprecated use hasPublicFields(String...) instead.
public SELF

Returns:

this assertions object
hasMethods
(String...
the method names which must be in the class.
methodNames
)

Verifies that the actual Class has the given methods (including inherited) whatever their visibility are.

public SELF

Returns:

this assertions object
hasNoPackage
()

Verifies that the actual Class has no package (as in Class#getPackage(), when null is returned).

public SELF

Returns:

this assertions object
hasNoSuperclass
()

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

public SELF

Returns:

this assertions object
hasOnlyDeclaredFields
(String...
all the fields that are expected to be in the class.
fields
)

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

public SELF

Returns:

this assertions object
hasOnlyPublicFields
(String...
all the fields that are expected to be in the class.
fields
)

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

public SELF

Returns:

this assertions object
hasPackage
(String
the package name the class should have
expected
)

Verifies that the actual Class has the given package name (as in Class#getPackage()).

public SELF

Returns:

this assertions object
hasPackage
(Package
the package the class should have
expected
)

Verifies that the actual Class has the given package (as in Class#getPackage()).

public SELF

Returns:

this assertions object
hasPermittedSubclasses
(Class<?>...
classes that must be permitted subclasses of the given class
permittedSubclasses
)

Verifies that the actual Class permitted subclasses contains the given classes.

public SELF

Returns:

this assertions object
hasPublicFields
(String...
the fields who must be in the class.
fields
)

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

public SELF

Returns:

this assertions object
hasPublicMethods
(String...
the public method names which must be in the class.
methodNames
)

Verifies that the actual Class has the given public methods.

public SELF

Returns:

this assertions object
hasRecordComponents
(String
the first record component name which must be in this class
first
,
String...
the remaining record component names which must be in this class
rest
)

Verifies that the actual Class has the given record components

Example:

 public class NotARecord {}

public record MyRecord(String componentOne, String componentTwo) {}

// these assertions succeed:
assertThat(MyRecord.class).hasRecordComponents("componentOne");
assertThat(MyRecord.class).hasRecordComponents("componentOne", "componentTwo");

// these assertions fail:
assertThat(NotARecord.class).hasRecordComponents("componentOne");
assertThat(MyRecord.class).hasRecordComponents("componentOne", "unknownComponent");
public SELF

Returns:

this assertions object
hasSuperclass
(Class<?>
the class which must be the direct superclass of actual.
superclass
)

Verifies that the actual Class has the given class as direct superclass (as in Class#getSuperclass()).

public SELF

Returns:

this assertions object
isAbstract
()

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

public SELF

Returns:

this assertions object
isAnnotation
()

Verifies that the actual Class is an annotation.

public SELF

Returns:

this assertions object
isAssignableFrom
(Class<?>...
Class who can be assignable from.
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);
public SELF

Returns:

this assertions object
isAssignableTo
(Class<?>
Class who can be assignable to.
other
)

Verifies that the Class under test is assignable to the given Class.

public SELF

Returns:

this assertions object
isFinal
()

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

public SELF

Returns:

this assertions object
isInterface
()

Verifies that the actual Class is an interface.

public SELF

Returns:

this assertions object
isNotAnnotation
()

Verifies that the actual Class is not an annotation.

public SELF

Returns:

this assertions object
isNotFinal
()

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

public SELF

Returns:

this assertions object
isNotInterface
()

Verifies that the actual Class is not an interface.

public SELF

Returns:

this assertions object
isNotPrimitive
()

Verifies that the actual Class is not a primitive type.

public SELF

Returns:

this assertions object
isNotRecord
()

Verifies that the actual Class is not a record.

public SELF

Returns:

this assertions object
isNotSealed
()

Verifies that the actual Class is not sealed.

public SELF

Returns:

this assertions object
isNotStatic
()

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

public SELF

Returns:

this assertions object
isPackagePrivate
()

Verifies that the actual Class is package-private (i.e., has no explicit access level modifier).

public SELF

Returns:

this assertions object
isPrimitive
()

Verifies that the actual Class is a primitive type.

public SELF

Returns:

this assertions object
isPrivate
()

Verifies that the actual Class is private (has private modifier).

public SELF

Returns:

this assertions object
isProtected
()

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

public SELF

Returns:

this assertions object
isPublic
()

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

public SELF

Returns:

this assertions object
isRecord
()

Verifies that the actual Class is a record.

private static boolean
isRecord(Class<?> actual)

public SELF

Returns:

this assertions object
isSealed
()

Verifies that the actual Class is sealed.

private static boolean
isSealed(Class<?> actual)

public SELF

Returns:

this assertions object
isStatic
()

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

Inherited from org.assertj.core.api.AbstractAssert:
areEqualasInstanceOfasListassertionErrorasStringdescribedAsdescriptionTextdoesNotHavedoesNotHaveSameClassAsdoesNotHaveSameHashCodeAsdoesNotHaveToStringdoesNotHaveToStringequalsextractingextractingfailurefailureWithActualExpectedfailWithActualExpectedAndMessagefailWithMessagegetWritableAssertionInfohashashCodehasSameClassAshasSameHashCodeAshasToStringhasToStringinBinaryinHexadecimalisisElementOfCustomAssertisEqualToisExactlyInstanceOfisInisInisInstanceOfisInstanceOfAnyisInstanceOfSatisfyingisNotisNotEqualToisNotExactlyInstanceOfisNotInisNotInisNotInstanceOfisNotInstanceOfAnyisNotNullisNotOfAnyClassInisNotSameAsisNullisOfAnyClassInisSameAsmatchesmatchesnewListAssertInstanceoverridingErrorMessageoverridingErrorMessagesatisfiessatisfiessatisfiessatisfiesAnyOfsatisfiesAnyOfsatisfiesAnyOfForProxysatisfiesForProxysetCustomRepresentationsetDescriptionConsumersetPrintAssertionsDescriptionthrowAssertionErrorusingComparatorusingComparatorusingDefaultComparatorusingRecursiveAssertionusingRecursiveAssertionusingRecursiveComparisonusingRecursiveComparisonwithAssertionStatewithFailMessagewithFailMessagewithRepresentationwithThreadDumpOnError

Field Detail

classesback to summary
pack-priv Classes classes

Constructor Detail

AbstractClassAssertback to summary
protected AbstractClassAssert(Class<?> actual, Class<?> selfType)

Method Detail

assertHasNoPackageback to summary
private void assertHasNoPackage()
assertHasNoSuperclassback to summary
private void assertHasNoSuperclass()
assertHasPackageback to summary
private void assertHasPackage(String packageName)
assertHasPackageback to summary
private void assertHasPackage(Package expected)
assertHasPermittedSubclassesback to summary
private void assertHasPermittedSubclasses(Class<?>[] expectedPermittedSubclasses)
assertHasRecordComponentsback to summary
private void assertHasRecordComponents(String first, String[] rest)
assertHasSuperclassback to summary
private void assertHasSuperclass(Class<?> superclass)
assertIsAbstractback to summary
private void assertIsAbstract()
assertIsAnnotationback to summary
private void assertIsAnnotation()
assertIsAssignableToback to summary
private void assertIsAssignableTo(Class<?> other)
assertIsFinalback to summary
private void assertIsFinal()
assertIsInterfaceback to summary
private void assertIsInterface()
assertIsNotAnnotationback to summary
private void assertIsNotAnnotation()
assertIsNotFinalback to summary
private void assertIsNotFinal()
assertIsNotInterfaceback to summary
private void assertIsNotInterface()
assertIsNotRecordback to summary
private void assertIsNotRecord()
assertIsNotSealedback to summary
private void assertIsNotSealed()
assertIsNotStaticback to summary
private void assertIsNotStatic()
assertIsPackagePrivateback to summary
private void assertIsPackagePrivate()
assertIsPrivateback to summary
private void assertIsPrivate()
assertIsProtectedback to summary
private void assertIsProtected()
assertIsPublicback to summary
private void assertIsPublic()
assertIsRecordback to summary
private void assertIsRecord()
assertIsSealedback to summary
private void assertIsSealed()
assertIsStaticback to summary
private void assertIsStatic()
getPermittedSubclassesback to summary
private static Class<?>[] getPermittedSubclasses(Class<?> actual)
getRecordComponentNamesback to summary
private static Set<String> getRecordComponentNames(Class<?> actual)
hasAnnotationback to summary
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:Class<? extends Annotation>

annotations who must be attached to the class

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contains all of these annotations.
hasAnnotationsback to summary
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:Class<? extends Annotation>[]

annotations who must be attached to the class

Returns:SELF

this assertions object

Annotations
@SafeVarargs
Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contains all of these annotations.
hasAnnotationsForProxyback to summary
protected SELF hasAnnotationsForProxy(Class<? extends Annotation>[] annotations)
hasDeclaredFieldsback to summary
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:String[]

the fields who must be declared in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contains all of the field.
See Also
Class#getDeclaredField(String)
hasDeclaredMethodsback to summary
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:String[]

the method names which must be declared in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contains all of the given methods.
Since
2.7.0 / 3.7.0
hasFieldsback to summary
public SELF hasFields(String... fields)

Deprecated

use hasPublicFields(String...) instead.

Parameters
fields:String[]

the fields who must be in the class.

Returns:SELF

this assertions object

Annotations
@Deprecated
hasMethodsback to summary
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:String[]

the method names which must be in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contains all of the method names.
Since
2.7.0 / 3.7.0
hasNoPackageback to summary
public SELF hasNoPackage()

Verifies that the actual Class has no package (as in Class#getPackage(), when null is returned).

Example:

 // this assertion succeeds as arrays have no package:
assertThat(int[].class).hasNoPackage();

// this assertion succeeds as primitive types have no package:
assertThat(Integer.TYPE).hasNoPackage();

// this assertion succeeds as void type has no package:
assertThat(Void.TYPE).hasNoPackage();

// this assertion fails as Object has java.lang as package:
assertThat(Object.class).hasNoPackage();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class has a package.
Since
3.25.0
See Also
hasPackage(Package), hasPackage(String)
hasNoSuperclassback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class has a superclass.
Since
3.15.0
See Also
hasSuperclass(Class)
hasOnlyDeclaredFieldsback to summary
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:String[]

all the fields that are expected to be in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if fields are not all the declared fields of the actual Class.
Since
2.7.0 / 3.7.0
See Also
Class#getField(String)
hasOnlyPublicFieldsback to summary
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:String[]

all the fields that are expected to be in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if fields are not all the actual Class's accessible public fields.
Since
2.7.0 / 3.7.0
See Also
Class#getField(String)
hasPackageback to summary
public SELF hasPackage(String expected)

Verifies that the actual Class has the given package name (as in Class#getPackage()).

The expected package name should always be not null. To verify the absence of the package, use hasNoPackage().

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
expected:String

the package name the class should have

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class does not have the given package.
Since
3.18.0
hasPackageback to summary
public SELF hasPackage(Package expected)

Verifies that the actual Class has the given package (as in Class#getPackage()).

The expected package should always be not null. To verify the absence of the package, use hasNoPackage().

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
expected:Package

the package the class should have

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class does not have the given package.
Since
3.18.0
See Also
hasPackage(String), hasNoPackage()
hasPermittedSubclassesback to summary
public SELF hasPermittedSubclasses(Class<?>... permittedSubclasses)

Verifies that the actual Class permitted subclasses contains the given classes.

Example:

 sealed class SealedClass permits NonSealedClass, FinalClass {}
non-sealed class NonSealedClass extends SealedClass {}
final class FinalClass extends SealedClass {}

// these assertions succeed:
assertThat(SealedClass.class).hasPermittedSubclasses(NonSealedClass.class)
                             .hasPermittedSubclasses(FinalClass.class)
                             .hasPermittedSubclasses(NonSealedClass.class, FinalClass.class)
                             .hasPermittedSubclasses();

// these assertions fail:
assertThat(SealedClass.class).hasPermittedSubclasses(String.class);
assertThat(SealedClass.class).hasPermittedSubclasses(FinalClass.class, String.class);
Parameters
permittedSubclasses:Class<?>[]

classes that must be permitted subclasses of the given class

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class does not have all of given permitted subclasses
hasPublicFieldsback to summary
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:String[]

the fields who must be in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contain all of the fields.
See Also
Class#getField(String)
hasPublicMethodsback to summary
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:String[]

the public method names which must be in the class.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class doesn't contains all of the given public methods.
Since
2.7.0 / 3.7.0
hasRecordComponentsback to summary
public SELF hasRecordComponents(String first, String... rest)

Verifies that the actual Class has the given record components

Example:

 public class NotARecord {}

public record MyRecord(String componentOne, String componentTwo) {}

// these assertions succeed:
assertThat(MyRecord.class).hasRecordComponents("componentOne");
assertThat(MyRecord.class).hasRecordComponents("componentOne", "componentTwo");

// these assertions fail:
assertThat(NotARecord.class).hasRecordComponents("componentOne");
assertThat(MyRecord.class).hasRecordComponents("componentOne", "unknownComponent");
Parameters
first:String

the first record component name which must be in this class

rest:String[]

the remaining record component names which must be in this class

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not a record.
  • if the actual Class doesn't contain all the record component names.
Since
3.25.0
hasSuperclassback to summary
public SELF hasSuperclass(Class<?> superclass)

Verifies that the actual Class has the given class as direct superclass (as in Class#getSuperclass()).

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

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:Class<?>

the class which must be the direct superclass of actual.

Returns:SELF

this assertions object

Exceptions
NullPointerException:
if superclass is null.
AssertionError:
  • if actual is null.
  • if the actual Class doesn't have the given class as direct superclass.
Since
3.15.0
See Also
hasNoSuperclass()
isAbstractback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not abstract.
Since
3.12.0
isAnnotationback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not an annotation.
isAssignableFromback to summary
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<?>[]

Class who can be assignable from.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if the actual Class is null.
  • if the actual Class is not assignable from all of the others classes.
IllegalArgumentException:
if no others classes have been specified.
See Also
Class#isAssignableFrom(Class)
isAssignableToback to summary
public SELF isAssignableTo(Class<?> other)

Verifies that the Class under test is assignable to the given Class.

Example:

 class Jedi {}
class HumanJedi extends Jedi {}

// this assertion succeeds:
assertThat(HumanJedi.class).isAssignableTo(Jedi.class);

// this assertion fails
assertThat(Jedi.class).isAssignableTo(HumanJedi.class);
Parameters
other:Class<?>

Class who can be assignable to.

Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if the actual Class is null.
  • if the actual Class is not assignable to the others class.
IllegalArgumentException:
if other is null.
Since
3.24.0
See Also
Class#isAssignableFrom(Class)
isFinalback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not final.
isInterfaceback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not an interface.
isNotAnnotationback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is an annotation.
isNotFinalback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is final.
isNotInterfaceback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not an interface.
isNotPrimitiveback to summary
public SELF isNotPrimitive()

Verifies that the actual Class is not a primitive type.

Example:

 // this assertion succeeds as Object is not a primitive type:
assertThat(Object.class).isNotPrimitive();

// these assertions fail:
assertThat(byte.class).isNotPrimitive();
assertThat(short.class).isNotPrimitive();
assertThat(int.class).isNotPrimitive();
assertThat(long.class).isNotPrimitive();
assertThat(float.class).isNotPrimitive();
assertThat(double.class).isNotPrimitive();
assertThat(boolean.class).isNotPrimitive();
assertThat(char.class).isNotPrimitive(); 
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is a primitive type.
See Also
Class#isPrimitive()
isNotRecordback to summary
public SELF isNotRecord()

Verifies that the actual Class is not a record.

Example:

 public record Jedi(String name) {}

// this assertion succeeds:
assertThat(String.class).isNotRecord();

// this assertion fails:
assertThat(Jedi.class).isNotRecord();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is a record.
Since
3.25.0
isNotSealedback to summary
public SELF isNotSealed()

Verifies that the actual Class is not sealed.

Example:

 sealed class SealedClass permits NonSealedClass {}

non-sealed class NonSealedClass extends SealedClass {}

// this assertion succeeds:
assertThat(NonSealedClass.class).isNotSealed();

// this assertion fails:
assertThat(SealedClass.class).isNotSealed();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is sealed.
Since
3.25.0
isNotStaticback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is static.
Since
3.23.0
isPackagePrivateback to summary
public SELF isPackagePrivate()

Verifies that the actual Class is package-private (i.e., has no explicit access level modifier).

Example:

 class MyClass { }

// this assertion succeeds:
assertThat(MyClass.class).isPackagePrivate();

// these assertions fail:
assertThat(String.class).isPackagePrivate();
assertThat(Math.class).isPackagePrivate();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not package-private.
Since
3.15.0
isPrimitiveback to summary
public SELF isPrimitive()

Verifies that the actual Class is a primitive type.

Example:

 // these assertions succeed:
assertThat(byte.class).isPrimitive();
assertThat(short.class).isPrimitive();
assertThat(int.class).isPrimitive();
assertThat(long.class).isPrimitive();
assertThat(float.class).isPrimitive();
assertThat(double.class).isPrimitive();
assertThat(boolean.class).isPrimitive();
assertThat(char.class).isPrimitive();

// this assertion fails as Object is not a primitive type:
assertThat(Object.class).isPrimitive(); 
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not a primitive type.
See Also
Class#isPrimitive()
isPrivateback to summary
public SELF isPrivate()

Verifies that the actual Class is private (has private modifier).

Example:

 class EnclosingClass {
  private static class PrivateClass { }
}

// these assertions succeed:
assertThat(PrivateClass.class).isPrivate();
assertThat(Class.forName(EnclosingClass.class.getName() + "$PrivateClass")).isPrivate();

// This assertion fails:
assertThat(String.class).isPrivate();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not private.
Since
3.26.0
isProtectedback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not protected.
Since
2.7.0 / 3.7.0
isPublicback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not public.
Since
2.7.0 / 3.7.0
isRecordback to summary
public SELF isRecord()

Verifies that the actual Class is a record.

Example:

 public record Jedi(String name) {}

// this assertion succeeds:
assertThat(Jedi.class).isRecord();

// this assertion fails:
assertThat(String.class).isRecord();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not a record.
Since
3.25.0
isRecordback to summary
private static boolean isRecord(Class<?> actual)
isSealedback to summary
public SELF isSealed()

Verifies that the actual Class is sealed.

Example:

 sealed class SealedClass permits NonSealedClass {}

non-sealed class NonSealedClass extends SealedClass {}

// this assertion succeeds:
assertThat(SealedClass.class).isSealed();

// this assertion fails:
assertThat(NonSealedClass.class).isSealed();
Returns:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not sealed.
Since
3.25.0
isSealedback to summary
private static boolean isSealed(Class<?> actual)
isStaticback to summary
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:SELF

this assertions object

Exceptions
AssertionError:
  • if actual is null.
  • if the actual Class is not static.
Since
3.23.0