Package org.assertj.core.api
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,ProxyableClassAssert
public abstract class AbstractClassAssert<SELF extends AbstractClassAssert<SELF>> extends AbstractAssert<SELF,Class<?>>
Base class for all implementations of assertions forClasses.- Author:
- William Delanoue, Mikhail Mazursky
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Classesclasses-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Constructor Description AbstractClassAssert(Class<?> actual, Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SELFhasAnnotation(Class<? extends Annotation> annotation)Verifies that the actualClasshas the givenAnnotation.SELFhasAnnotations(Class<? extends Annotation>... annotations)Verifies that the actualClasshas the givenAnnotations.SELFhasDeclaredFields(String... fields)Verifies that the actualClasshas the given declared fields (as inClass.getDeclaredFields()).SELFhasDeclaredMethods(String... methodNames)Verifies that the actualClasshas the given declared methods.SELFhasFields(String... fields)Deprecated.usehasPublicFields(String...)instead.SELFhasMethods(String... methodNames)Verifies that the actualClasshas the given methods (including inherited) whatever their visibility are.SELFhasOnlyDeclaredFields(String... fields)Verifies that the actualClassonly has the given declaredfieldsand nothing more in any order (as inClass.getDeclaredFields()).SELFhasOnlyPublicFields(String... fields)Verifies that the actualClassonly has the given accessible public fields (as inClass.getFields()) and nothing more in any order.SELFhasPublicFields(String... fields)Verifies that the actualClasshas the given accessible public fields (as inClass.getFields()).SELFhasPublicMethods(String... methodNames)Verifies that the actualClasshas the given public methods.SELFisAbstract()Verifies that the actualClassis abstract (hasabstractmodifier).SELFisAnnotation()Verifies that the actualClassis an annotation.SELFisAssignableFrom(Class<?>... others)Verifies that the actualClassis assignable from othersClassSELFisFinal()Verifies that the actualClassis final (hasfinalmodifier).SELFisInterface()Verifies that the actualClassis an interface.SELFisNotAnnotation()Verifies that the actualClassis not an annotation.SELFisNotFinal()Verifies that the actualClassis not final (does not havefinalmodifier).SELFisNotInterface()Verifies that the actualClassis not an interface.SELFisProtected()Verifies that the actualClassis protected (hasprotectedmodifier).SELFisPublic()Verifies that the actualClassis public (haspublicmodifier).-
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, asInstanceOf, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withAssertionState, withFailMessage, withRepresentation, withThreadDumpOnError
-
-
-
-
Field Detail
-
classes
Classes classes
-
-
Method Detail
-
isAssignableFrom
public SELF isAssignableFrom(Class<?>... others)
Verifies that the actualClassis assignable from othersClassExample:
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-Classwho can be assignable from.- Returns:
thisassertions object- Throws:
AssertionError- if the actualClassisnull.IllegalArgumentException- if noothersclasses have been specified.AssertionError- if the actualClassis not assignable from all of theothersclasses.- See Also:
Class.isAssignableFrom(Class)
-
isNotInterface
public SELF isNotInterface()
Verifies that the actualClassis 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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not an interface.
-
isInterface
public SELF isInterface()
Verifies that the actualClassis an interface.Example:
interface Jedi {} class HumanJedi implements Jedi {} // this assertion succeeds: assertThat(Jedi.class).isInterface(); // this assertion fails: assertThat(HumanJedi.class).isInterface();- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not an interface.
-
isAbstract
public SELF isAbstract()
Verifies that the actualClassis abstract (hasabstractmodifier).Example:
public abstract class MyClass { } // this assertion succeeds: assertThat(MyClass.class).isAbstract(); // this assertion fails: assertThat(String.class).isAbstract();- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not abstract.- Since:
- 3.12.0
-
isAnnotation
public SELF isAnnotation()
Verifies that the actualClassis 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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not an annotation.
-
isNotAnnotation
public SELF isNotAnnotation()
Verifies that the actualClassis 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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis an annotation.
-
isFinal
public SELF isFinal()
Verifies that the actualClassis final (hasfinalmodifier).Example:
// these assertions succeed: assertThat(String.class).isFinal(); assertThat(Math.class).isFinal(); // these assertions fail: assertThat(Object.class).isFinal(); assertThat(Throwable.class).isFinal();- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not final.
-
isNotFinal
public SELF isNotFinal()
Verifies that the actualClassis not final (does not havefinalmodifier).Example:
// these assertions succeed: assertThat(Object.class).isNotFinal(); assertThat(Throwable.class).isNotFinal(); // these assertions fail: assertThat(String.class).isNotFinal(); assertThat(Math.class).isNotFinal();- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis final.
-
isPublic
public SELF isPublic()
Verifies that the actualClassis public (haspublicmodifier).Example:
protected class MyClass { } // these assertions succeed: assertThat(String.class).isPublic(); assertThat(Math.class).isPublic(); // This assertion fails: assertThat(MyClass.class).isPublic();- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not public.- Since:
- 2.7.0 / 3.7.0
-
isProtected
public SELF isProtected()
Verifies that the actualClassis protected (hasprotectedmodifier).Example:
public class MyClass { } // this assertion succeeds: assertThat(MyClass.class).isProtected(); // these assertions fail: assertThat(String.class).isProtected(); assertThat(Math.class).isProtected();- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassis not protected.- Since:
- 2.7.0 / 3.7.0
-
hasAnnotations
public SELF hasAnnotations(Class<? extends Annotation>... annotations)
Verifies that the actualClasshas the givenAnnotations.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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contains all of these annotations.
-
hasAnnotation
public SELF hasAnnotation(Class<? extends Annotation> annotation)
Verifies that the actualClasshas 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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contains all of these annotations.
-
hasFields
@Deprecated public SELF hasFields(String... fields)
Deprecated.usehasPublicFields(String...)instead.- Parameters:
fields- the fields who must be in the class.- Returns:
thisassertions object
-
hasPublicFields
public SELF hasPublicFields(String... fields)
Verifies that the actualClasshas 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
Classhas no accessible public fields.- Parameters:
fields- the fields who must be in the class.- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contain all of the fields.- See Also:
Class.getField(String)
-
hasOnlyPublicFields
public SELF hasOnlyPublicFields(String... fields)
Verifies that the actualClassonly 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
Classhas no accessible public fields.- Parameters:
fields- all the fields that are expected to be in the class.- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.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(String... fields)
Verifies that the actualClasshas 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
Classhas no declared fields.- Parameters:
fields- the fields who must be declared in the class.- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contains all of the field.- See Also:
Class.getDeclaredField(String)
-
hasOnlyDeclaredFields
public SELF hasOnlyDeclaredFields(String... fields)
Verifies that the actualClassonly has the given declaredfieldsand 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
Classhas no declared fields.- Parameters:
fields- all the fields that are expected to be in the class.- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.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(String... methodNames)
Verifies that the actualClasshas 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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contains all of the method names.- Since:
- 2.7.0 / 3.7.0
-
hasDeclaredMethods
public SELF hasDeclaredMethods(String... methodNames)
Verifies that the actualClasshas 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
Classhas no declared methods.- Parameters:
methodNames- the method names which must be declared in the class.- Returns:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contains all of the given methods.- Since:
- 2.7.0 / 3.7.0
-
hasPublicMethods
public SELF hasPublicMethods(String... methodNames)
Verifies that the actualClasshas 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:
thisassertions object- Throws:
AssertionError- ifactualisnull.AssertionError- if the actualClassdoesn't contains all of the given public methods.- Since:
- 2.7.0 / 3.7.0
-
-