Class AssertionsForInterfaceTypes


  • public class AssertionsForInterfaceTypes
    extends AssertionsForClassTypes
    Entry point for assertion methods for different data types. Each method in this class is a static factory for the type-specific assertion objects. The purpose of this class is to make test code more readable.

    For example:

     int removed = employees.removeFired();
     assertThat(removed).isZero();
    
     List<Employee> newEmployees = employees.hired(TODAY);
     assertThat(newEmployees).hasSize(6); 
    Java 8 is picky when choosing the right assertThat method if the object under test is generic and bounded, for example if foo is instance of T that extends Exception, java 8 will complain that it can't resolve the proper assertThat method (normally assertThat(Throwable) as foo might implement an interface like List, if that occurred assertThat(List) would also be a possible choice - thus confusing java 8.

    This why Assertions have been split in AssertionsForClassTypes and AssertionsForInterfaceTypes (see http://stackoverflow.com/questions/29499847/ambiguous-method-in-java-8-why).

    Author:
    Alex Ruiz, Yvonne Wang, David DIDIER, Ted Young, Joel Costigliola, Matthieu Baechler, Mikhail Mazursky, Nicolas François, Julien Meddah, William Delanoue, Turbo87, dorzey
    • Constructor Detail

      • AssertionsForInterfaceTypes

        protected AssertionsForInterfaceTypes()
        Creates a new Assertions.
    • Method Detail

      • assertThat

        public static <T> T assertThat​(AssertProvider<T> component)
        Delegates the creation of the Assert to the AssertProvider.assertThat() of the given component.

        Read the comments on AssertProvider for an example of its usage.

        Parameters:
        component - the component that creates its own assert
        Returns:
        the associated Assert of the given component
      • assertThat

        public static <ELEMENT> IterableAssert<ELEMENT> assertThat​(Iterable<? extends ELEMENT> actual)
        Creates a new instance of IterableAssert.
        Parameters:
        actual - the actual value.
        Returns:
        the created assertion object.
      • assertThat

        public static <ELEMENT> IterableAssert<ELEMENT> assertThat​(Iterator<? extends ELEMENT> actual)
        Creates a new instance of IterableAssert.

        Be aware that calls to most methods on returned IterableAssert will consume Iterator so it won't be possible to iterate over it again. Calling multiple methods on returned IterableAssert is safe as Iterator's elements are cached by IterableAssert first time Iterator is consumed.

        Parameters:
        actual - the actual value.
        Returns:
        the created assertion object.
      • assertThat

        public static <ELEMENT> ListAssert<ELEMENT> assertThat​(List<? extends ELEMENT> actual)
        Creates a new instance of ListAssert.
        Parameters:
        actual - the actual value.
        Returns:
        the created assertion object.
      • assertThat

        public static <ELEMENT,​ACTUAL extends List<? extends ELEMENT>,​ELEMENT_ASSERT extends AbstractAssert<ELEMENT_ASSERT,​ELEMENT>> ClassBasedNavigableListAssert<?,​ACTUAL,​ELEMENT,​ELEMENT_ASSERT> assertThat​(List<? extends ELEMENT> actual,
                                                                                                                                                                                                                                                   Class<ELEMENT_ASSERT> assertClass)
      • assertThat

        public static AbstractPathAssert<?> assertThat​(Path actual)
        Creates a new instance of PathAssert
        Parameters:
        actual - the path to test
        Returns:
        the created assertion object
      • assertThat

        public static <K,​V> MapAssert<K,​V> assertThat​(Map<K,​V> actual)
        Creates a new instance of MapAssert.

        Returned type is MapAssert as it overrides method to annotate them with SafeVarargs avoiding annoying warnings.

        Parameters:
        actual - the actual value.
        Returns:
        the created assertion object.
      • assertThat

        public static <T extends Comparable<? super T>> AbstractComparableAssert<?,​T> assertThat​(T actual)
        Creates a new instance of GenericComparableAssert with standard comparison semantics.
        Parameters:
        actual - the actual value.
        Returns:
        the created assertion object.
      • assertThat

        public static <T extends AssertDelegateTarget> T assertThat​(T assertion)
        Returns the given assertion. This method improves code readability by surrounding the given assertion with assertThat.

        Consider for example the following MyButton and MyButtonAssert classes:

         public class MyButton extends JButton {
        
           private boolean blinking;
        
           public boolean isBlinking() { return this.blinking; }
        
           public void setBlinking(boolean blink) { this.blinking = blink; }
        
         }
        
         private static class MyButtonAssert implements AssertDelegateTarget {
        
           private MyButton button;
           MyButtonAssert(MyButton button) { this.button = button; }
        
           void isBlinking() {
             // standard assertion from core Assertions.assertThat
             assertThat(button.isBlinking()).isTrue();
           }
        
           void isNotBlinking() {
             // standard assertion from core Assertions.assertThat
             assertThat(button.isBlinking()).isFalse();
           }
         }
        As MyButtonAssert implements AssertDelegateTarget, you can use assertThat(buttonAssert).isBlinking(); instead of buttonAssert.isBlinking(); to have easier to read assertions:
         @Test
         public void AssertDelegateTarget_example() {
        
           MyButton button = new MyButton();
           MyButtonAssert buttonAssert = new MyButtonAssert(button);
        
           // you can encapsulate MyButtonAssert assertions methods within assertThat
           assertThat(buttonAssert).isNotBlinking(); // same as : buttonAssert.isNotBlinking();
        
           button.setBlinking(true);
        
           assertThat(buttonAssert).isBlinking(); // same as : buttonAssert.isBlinking();
         }
        Type Parameters:
        T - the generic type of the user-defined assert.
        Parameters:
        assertion - the assertion to return.
        Returns:
        the given assertion.
      • assertThat

        public static <T> PredicateAssert<T> assertThat​(Predicate<T> actual)
        Create assertion for Predicate.
        Type Parameters:
        T - the type of the value contained in the Predicate.
        Parameters:
        actual - the actual value.
        Returns:
        the created assertion object.
        Since:
        3.5.0