Add more assertion on ClassAssert
See original GitHub issueSummary
Apologies, If I’m wrong.
It’s nice that the ClassAssert
already has an assertion for Class#isAssignableFrom.
An assertion of opposite flow may be more intuitive, I believe.
Say, we have the following class or any other class being expected to implement the MyInterface
interface.
class MyClass
extends HisClass // which extends HerClass
implements MyInterface {
}
HerClass
HisClass
MyClass
We can,
// Asserts that MyInterface.class is assignable from MyClass.class, based on Class#isAssignableFrom
assertThat(MyInterface.class)
.isAssignableFrom(MyClass.class);
// Asserts that MyClass.class has HisClass.class as direct superclass.
assertThat(MyClass.class)
.hasSuperclass(HisClass.class);
But following idiom(s) will be more intuitive.
// Asserts that MyClass implements MyInterface
assertThat(MyClass.class)
.implements(MyInterface.class)
.isAssignableTo(MyInterface.class);
// Or
assertThat(MyClass.class)
.implements(MyInterface.class)
.isSubclassOf(HisClass.class) // is direct subclass; opposite of hasSuperclass(superClass)
.isDescendentOf(HisClass.class)
.isDescendentOf(HerClass.class) // is direct or transitively
;
assertThat(HisClass.class)
.isAscendentOf(MyClass.class)
.isSubclassOf(HerClass.class)
.isDescendentOf(HerClass.class);
-
isAssignableTo(classOrInterface)
-
implements(interfaceClass)
-
isSubclassOf(superClass)
- is direct subclass of -
is(Descendant|Descendent)Of(ascendentClass)
- is direct or transitive descendent of -
is(Ancestor|Ascendent)Of(descendentClass)
- is direct or transitive ascendent of
Issue Analytics
- State:
- Created a year ago
- Comments:20 (18 by maintainers)
Top Results From Across the Web
Add more assertion on ClassAssert · Issue #2570 - GitHub
It's nice that the ClassAssert already has an assertion for Class#isAssignableFrom. An assertion of opposite flow may be more intuitive, I ...
Read more >Extending XUnit Assert class with new asserts - Stack Overflow
In the Xunit namespace, define public partial class Assert and add your custom asserts there. In your test project install the xunit.extensibility.execution ...
Read more >ClassAssert (AssertJ fluent assertions 3.12.0 API)
Verifies that the actual Class has the given Annotation s. Example: @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) private static @interface ...
Read more >JUnit - Using Assertion - Tutorialspoint
All the assertions are in the Assert class. public class Assert extends java.lang.Object. This class provides a set of assertion methods, useful for...
Read more >Assert (JUnit API)
public class Assert; extends Object. A set of assertion methods useful for writing tests. Only failed assertions are recorded. These methods can be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alternative naming proposal from https://github.com/assertj/assertj-core/issues/2545#issuecomment-1081352291 for the
isAssignableTo
assertion:isCompatibleWith
/isNotCompatibleWith
@scordio Sorry for the late call, but yes, I think
isAssignableTo
andisAssignableFrom
are good enough for now. 👍isSubclassOf
/hasParent
are quite interesting (but not directly required by me). I think they are unpleasantly “less precise” though; they first one would perhaps more rightfully beisDirectSubclassOf
since in anA -> B -> C
,C
is by definition a subclass ofA
as well as ofB
.hasParent
is perhaps less unambiguous in this since parents and grandparents are conceptually different. 🙂