Class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<SELF,java.lang.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,java.lang.Class<?>>
,Descriptable<SELF>
,ExtensionPoints<SELF,java.lang.Class<?>>
- Direct Known Subclasses:
ClassAssert
public abstract class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>> extends AbstractAssert<SELF,java.lang.Class<?>>
Base class for all implementations of assertions forClass
es.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Classes
classes
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, customRepresentation, info, myself, objects, printAssertionsDescription, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractClassAssert(java.lang.Class<?> actual, java.lang.Class<?> selfType)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private void
assertHasNoPackage()
private void
assertHasNoSuperclass()
private void
assertHasPackage(java.lang.Package expected)
private void
assertHasPackage(java.lang.String packageName)
private void
assertHasRecordComponents(java.lang.String first, java.lang.String[] rest)
private void
assertHasSuperclass(java.lang.Class<?> superclass)
private void
assertIsAbstract()
private void
assertIsAnnotation()
private void
assertIsAssignableTo(java.lang.Class<?> other)
private void
assertIsFinal()
private void
assertIsInterface()
private void
assertIsNotAnnotation()
private void
assertIsNotFinal()
private void
assertIsNotInterface()
private void
assertIsNotRecord()
private void
assertIsNotSealed()
private void
assertIsNotStatic()
private void
assertIsPackagePrivate()
private void
assertIsProtected()
private void
assertIsPublic()
private void
assertIsRecord()
private void
assertIsSealed()
private void
assertIsStatic()
private static java.util.Set<java.lang.String>
getRecordComponentNames(java.lang.Class<?> actual)
SELF
hasAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Verifies that the actualClass
has the givenAnnotation
.SELF
hasAnnotations(java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
Verifies that the actualClass
has the givenAnnotation
s.protected SELF
hasAnnotationsForProxy(java.lang.Class<? extends java.lang.annotation.Annotation>[] annotations)
SELF
hasDeclaredFields(java.lang.String... fields)
Verifies that the actualClass
has the given declared fields (as inClass.getDeclaredFields()
).SELF
hasDeclaredMethods(java.lang.String... methodNames)
Verifies that the actualClass
has the given declared methods.SELF
hasFields(java.lang.String... fields)
Deprecated.usehasPublicFields(String...)
instead.SELF
hasMethods(java.lang.String... methodNames)
Verifies that the actualClass
has the given methods (including inherited) whatever their visibility are.SELF
hasNoPackage()
Verifies that the actualClass
has no package (as inClass.getPackage()
, whennull
is returned).SELF
hasNoSuperclass()
Verifies that the actualClass
has no superclass (as inClass.getSuperclass()
, whennull
is returned).SELF
hasOnlyDeclaredFields(java.lang.String... fields)
Verifies that the actualClass
only has the given declaredfields
and nothing more in any order (as inClass.getDeclaredFields()
).SELF
hasOnlyPublicFields(java.lang.String... fields)
Verifies that the actualClass
only has the given accessible public fields (as inClass.getFields()
) and nothing more in any order.SELF
hasPackage(java.lang.Package expected)
Verifies that the actualClass
has the given package (as inClass.getPackage()
).SELF
hasPackage(java.lang.String expected)
Verifies that the actualClass
has the given package name (as inClass.getPackage()
).SELF
hasPublicFields(java.lang.String... fields)
Verifies that the actualClass
has the given accessible public fields (as inClass.getFields()
).SELF
hasPublicMethods(java.lang.String... methodNames)
Verifies that the actualClass
has the given public methods.SELF
hasRecordComponents(java.lang.String first, java.lang.String... rest)
Verifies that the actualClass
has the given record componentsSELF
hasSuperclass(java.lang.Class<?> superclass)
Verifies that the actualClass
has the given class as direct superclass (as inClass.getSuperclass()
).SELF
isAbstract()
Verifies that the actualClass
is abstract (hasabstract
modifier).SELF
isAnnotation()
Verifies that the actualClass
is an annotation.SELF
isAssignableFrom(java.lang.Class<?>... others)
Verifies that the actualClass
is assignable from othersClass
SELF
isAssignableTo(java.lang.Class<?> other)
Verifies that theClass
under test is assignable to the givenClass
.SELF
isFinal()
Verifies that the actualClass
is final (hasfinal
modifier).SELF
isInterface()
Verifies that the actualClass
is an interface.SELF
isNotAnnotation()
Verifies that the actualClass
is not an annotation.SELF
isNotFinal()
Verifies that the actualClass
is not final (does not havefinal
modifier).SELF
isNotInterface()
Verifies that the actualClass
is not an interface.SELF
isNotPrimitive()
Verifies that the actualClass
is not a primitive type.SELF
isNotRecord()
Verifies that the actualClass
is not a record.SELF
isNotSealed()
Verifies that the actualClass
is not sealed.SELF
isNotStatic()
Verifies that the actualClass
is not static (does not havestatic
modifier).SELF
isPackagePrivate()
Verifies that the actualClass
is package-private (has no modifier).SELF
isPrimitive()
Verifies that the actualClass
is a primitive type.SELF
isProtected()
Verifies that the actualClass
is protected (hasprotected
modifier).SELF
isPublic()
Verifies that the actualClass
is public (haspublic
modifier).SELF
isRecord()
Verifies that the actualClass
is a record.private static boolean
isRecord(java.lang.Class<?> actual)
SELF
isSealed()
Verifies that the actualClass
is sealed.private static boolean
isSealed(java.lang.Class<?> actual)
SELF
isStatic()
Verifies that the actualClass
is static (hasstatic
modifier).-
Methods inherited from class org.assertj.core.api.AbstractAssert
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison, withAssertionState, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.assertj.core.api.Descriptable
as, as, as, describedAs, describedAs
-
-
-
-
Field Detail
-
classes
Classes classes
-
-
Method Detail
-
isAssignableFrom
public SELF isAssignableFrom(java.lang.Class<?>... others)
Verifies that the actualClass
is assignable from othersClass
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:
java.lang.AssertionError
- if the actualClass
isnull
.java.lang.IllegalArgumentException
- if noothers
classes have been specified.java.lang.AssertionError
- if the actualClass
is not assignable from all of theothers
classes.- See Also:
Class.isAssignableFrom(Class)
-
isAssignableTo
public SELF isAssignableTo(java.lang.Class<?> other)
Verifies that theClass
under test is assignable to the givenClass
.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
who can be assignable to.- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- if the actualClass
isnull
.java.lang.IllegalArgumentException
- ifother
is null.java.lang.AssertionError
- if the actualClass
is not assignable to theothers
class.- Since:
- 3.24.0
- See Also:
Class.isAssignableFrom(Class)
-
assertIsAssignableTo
private void assertIsAssignableTo(java.lang.Class<?> other)
-
isNotInterface
public SELF isNotInterface()
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not an interface.
-
assertIsNotInterface
private void assertIsNotInterface()
-
isInterface
public SELF isInterface()
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not an interface.
-
assertIsInterface
private void assertIsInterface()
-
isAbstract
public SELF isAbstract()
Verifies that the actualClass
is abstract (hasabstract
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not abstract.- Since:
- 3.12.0
-
assertIsAbstract
private void assertIsAbstract()
-
isAnnotation
public SELF isAnnotation()
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not an annotation.
-
assertIsAnnotation
private void assertIsAnnotation()
-
isNotAnnotation
public SELF isNotAnnotation()
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is an annotation.
-
assertIsNotAnnotation
private void assertIsNotAnnotation()
-
isRecord
public SELF isRecord()
Verifies that the actualClass
is a record.Example:
public record Jedi(String name) {} // this assertion succeeds: assertThat(Jedi.class).isRecord(); // this assertion fails: assertThat(String.class).isRecord();
- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not a record.- Since:
- 3.25.0
-
assertIsRecord
private void assertIsRecord()
-
isNotRecord
public SELF isNotRecord()
Verifies that the actualClass
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:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is a record.- Since:
- 3.25.0
-
assertIsNotRecord
private void assertIsNotRecord()
-
isRecord
private static boolean isRecord(java.lang.Class<?> actual)
-
hasRecordComponents
public SELF hasRecordComponents(java.lang.String first, java.lang.String... rest)
Verifies that the actualClass
has the given record componentsExample:
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
- the first record component name which must be in this classrest
- the remaining record component names which must be in this class- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not a record.java.lang.AssertionError
- if the actualClass
doesn't contain all the record component names.- Since:
- 3.25.0
-
assertHasRecordComponents
private void assertHasRecordComponents(java.lang.String first, java.lang.String[] rest)
-
getRecordComponentNames
private static java.util.Set<java.lang.String> getRecordComponentNames(java.lang.Class<?> actual)
-
isFinal
public SELF isFinal()
Verifies that the actualClass
is final (hasfinal
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not final.
-
assertIsFinal
private void assertIsFinal()
-
isNotFinal
public SELF isNotFinal()
Verifies that the actualClass
is not final (does not havefinal
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is final.
-
assertIsNotFinal
private void assertIsNotFinal()
-
isPublic
public SELF isPublic()
Verifies that the actualClass
is public (haspublic
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not public.- Since:
- 2.7.0 / 3.7.0
-
assertIsPublic
private void assertIsPublic()
-
isProtected
public SELF isProtected()
Verifies that the actualClass
is protected (hasprotected
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not protected.- Since:
- 2.7.0 / 3.7.0
-
assertIsProtected
private void assertIsProtected()
-
isPackagePrivate
public SELF isPackagePrivate()
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not package-private.- Since:
- 3.15.0
-
assertIsPackagePrivate
private void assertIsPackagePrivate()
-
isStatic
public SELF isStatic()
Verifies that the actualClass
is static (hasstatic
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not static.- Since:
- 3.23.0
-
assertIsStatic
private void assertIsStatic()
-
isNotStatic
public SELF isNotStatic()
Verifies that the actualClass
is not static (does not havestatic
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is static.- Since:
- 3.23.0
-
assertIsNotStatic
private void assertIsNotStatic()
-
hasAnnotations
@SafeVarargs public final SELF hasAnnotations(java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
Verifies that the actualClass
has the givenAnnotation
s.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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contains all of these annotations.
-
hasAnnotationsForProxy
protected SELF hasAnnotationsForProxy(java.lang.Class<? extends java.lang.annotation.Annotation>[] annotations)
-
hasAnnotation
public SELF hasAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Verifies that the actualClass
has the givenAnnotation
.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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contains all of these annotations.
-
hasSuperclass
public SELF hasSuperclass(java.lang.Class<?> superclass)
Verifies that the actualClass
has the given class as direct superclass (as inClass.getSuperclass()
).The expected
superclass
should always be notnull
. To verify the absence of the superclass, usehasNoSuperclass()
.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:
java.lang.NullPointerException
- ifsuperclass
isnull
.java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't have the given class as direct superclass.- Since:
- 3.15.0
- See Also:
hasNoSuperclass()
-
assertHasSuperclass
private void assertHasSuperclass(java.lang.Class<?> superclass)
-
hasNoSuperclass
public SELF hasNoSuperclass()
Verifies that the actualClass
has no superclass (as inClass.getSuperclass()
, whennull
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
has a superclass.- Since:
- 3.15.0
- See Also:
hasSuperclass(Class)
-
assertHasNoSuperclass
private void assertHasNoSuperclass()
-
hasFields
@Deprecated public SELF hasFields(java.lang.String... fields)
Deprecated.usehasPublicFields(String...)
instead.- Parameters:
fields
- the fields who must be in the class.- Returns:
this
assertions object
-
hasPublicFields
public SELF hasPublicFields(java.lang.String... fields)
Verifies that the actualClass
has the given accessible public fields (as inClass.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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contain all of the fields.- See Also:
Class.getField(String)
-
hasOnlyPublicFields
public SELF hasOnlyPublicFields(java.lang.String... fields)
Verifies that the actualClass
only has the given accessible public fields (as inClass.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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if fields are not all the actualClass
's accessible public fields.- Since:
- 2.7.0 / 3.7.0
- See Also:
Class.getField(String)
-
hasDeclaredFields
public SELF hasDeclaredFields(java.lang.String... fields)
Verifies that the actualClass
has the given declared fields (as inClass.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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contains all of the field.- See Also:
Class.getDeclaredField(String)
-
hasOnlyDeclaredFields
public SELF hasOnlyDeclaredFields(java.lang.String... fields)
Verifies that the actualClass
only has the given declaredfields
and nothing more in any order (as inClass.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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if fields are not all the declared fields of the actualClass
.- Since:
- 2.7.0 / 3.7.0
- See Also:
Class.getField(String)
-
hasMethods
public SELF hasMethods(java.lang.String... methodNames)
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contains all of the method names.- Since:
- 2.7.0 / 3.7.0
-
hasDeclaredMethods
public SELF hasDeclaredMethods(java.lang.String... methodNames)
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contains all of the given methods.- Since:
- 2.7.0 / 3.7.0
-
hasPublicMethods
public SELF hasPublicMethods(java.lang.String... methodNames)
Verifies that the actualClass
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:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
doesn't contains all of the given public methods.- Since:
- 2.7.0 / 3.7.0
-
hasPackage
public SELF hasPackage(java.lang.String expected)
Verifies that the actualClass
has the given package name (as inClass.getPackage()
).The expected package name should always be not
null
. To verify the absence of the package, usehasNoPackage()
.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
- the package name the class should have- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
does not have the given package.- Since:
- 3.18.0
-
assertHasPackage
private void assertHasPackage(java.lang.String packageName)
-
hasPackage
public SELF hasPackage(java.lang.Package expected)
Verifies that the actualClass
has the given package (as inClass.getPackage()
).The expected package should always be not
null
. To verify the absence of the package, usehasNoPackage()
.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
- the package the class should have- Returns:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
does not have the given package.- Since:
- 3.18.0
- See Also:
hasPackage(String)
,hasNoPackage()
-
assertHasPackage
private void assertHasPackage(java.lang.Package expected)
-
hasNoPackage
public SELF hasNoPackage()
Verifies that the actualClass
has no package (as inClass.getPackage()
, whennull
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:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
has a package.- Since:
- 3.25.0
- See Also:
hasPackage(Package)
,hasPackage(String)
-
assertHasNoPackage
private void assertHasNoPackage()
-
isSealed
public SELF isSealed()
Verifies that the actualClass
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:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not sealed.- Since:
- 3.25.0
-
assertIsSealed
private void assertIsSealed()
-
isNotSealed
public SELF isNotSealed()
Verifies that the actualClass
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:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is sealed.- Since:
- 3.25.0
-
assertIsNotSealed
private void assertIsNotSealed()
-
isSealed
private static boolean isSealed(java.lang.Class<?> actual)
-
isPrimitive
public SELF isPrimitive()
Verifies that the actualClass
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:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is not a primitive type.- See Also:
Class.isPrimitive()
-
isNotPrimitive
public SELF isNotPrimitive()
Verifies that the actualClass
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:
this
assertions object- Throws:
java.lang.AssertionError
- ifactual
isnull
.java.lang.AssertionError
- if the actualClass
is a primitive type.- See Also:
Class.isPrimitive()
-
-