question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add more assertion on ClassAssert

See original GitHub issue

Summary

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:closed
  • Created a year ago
  • Comments:20 (18 by maintainers)

github_iconTop GitHub Comments

3reactions
scordiocommented, Apr 15, 2022

Alternative naming proposal from https://github.com/assertj/assertj-core/issues/2545#issuecomment-1081352291 for the isAssignableTo assertion:

  • isCompatibleWith / isNotCompatibleWith
2reactions
perluncommented, Nov 16, 2022

@scordio Sorry for the late call, but yes, I think isAssignableTo and isAssignableFrom 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 be isDirectSubclassOf since in an A -> B -> C, C is by definition a subclass of A as well as of B. hasParent is perhaps less unambiguous in this since parents and grandparents are conceptually different. 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found