Class FileAssert


public class FileAssert extends GenericAssert<File>
Understands assertion methods for File. To create a new instance of this class use the method Assertions.assertThat(File).
  • Field Details

  • Constructor Details

    • FileAssert

      protected FileAssert(File actual)
      Creates a new FileAssert.
      Parameters:
      actual - the target to verify.
    • FileAssert

      FileAssert(File actual, FileContentComparator comparator)
  • Method Details

    • as

      public FileAssert as(String description)
      Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).as("name").isEqualTo("Frodo");
       

      Specified by:
      as in class GenericAssert<File>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public FileAssert describedAs(String description)
      Alias for GenericAssert.as(String), since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).describedAs("name").isEqualTo("Frodo");
       

      Specified by:
      describedAs in class GenericAssert<File>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • as

      public FileAssert as(Description description)
      Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
       

      Specified by:
      as in class GenericAssert<File>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public FileAssert describedAs(Description description)
      Alias for GenericAssert.as(Description), since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
       

      Specified by:
      describedAs in class GenericAssert<File>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • doesNotExist

      public FileAssert doesNotExist()
      Verifies that the actual File does not exist.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the the actual File is null.
      AssertionError - if the actual File exists.
    • exists

      public FileAssert exists()
      Verifies that the actual File does exist.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the the actual File is null.
      AssertionError - if the actual File does not exist.
    • hasSize

      public FileAssert hasSize(long expected)
      Verifies that the size of the actual File is equal to the given one.
      Parameters:
      expected - the expected size of the actual File.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the the actual File is null.
      AssertionError - if the size of the actual File is not equal to the given one.
    • isDirectory

      public FileAssert isDirectory()
      Verifies that the actual File is a directory.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the the actual File is null.
      AssertionError - if the actual File is not a directory.
    • isEqualTo

      public FileAssert isEqualTo(File expected)
      Verifies that the actual File is equal to the given one. To verify that the actual File has the same content as another File, use hasSameContentAs(File).
      Specified by:
      isEqualTo in class GenericAssert<File>
      Parameters:
      expected - the given File to compare the actual File to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is not equal to the given one.
    • isFile

      public FileAssert isFile()
      Verifies that the actual File is a regular file.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the the actual File is null.
      AssertionError - if the actual File is not a regular file.
    • isNotEqualTo

      public FileAssert isNotEqualTo(File other)
      Verifies that the actual File is not equal to the given one.
      Specified by:
      isNotEqualTo in class GenericAssert<File>
      Parameters:
      other - the given File to compare the actual File to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is equal to the given one.
    • isNotNull

      public FileAssert isNotNull()
      Verifies that the actual File is not null.
      Specified by:
      isNotNull in class GenericAssert<File>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is null.
    • isNotSameAs

      public FileAssert isNotSameAs(File other)
      Verifies that the actual File is not the same as the given one.
      Specified by:
      isNotSameAs in class GenericAssert<File>
      Parameters:
      other - the given File to compare the actual File to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is the same as the given one.
    • isSameAs

      public FileAssert isSameAs(File expected)
      Verifies that the actual File is the same as the given one.
      Specified by:
      isSameAs in class GenericAssert<File>
      Parameters:
      expected - the given File to compare the actual File to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is not the same as the given one.
    • satisfies

      public FileAssert satisfies(Condition<File> condition)
      Verifies that the actual File satisfies the given condition.
      Specified by:
      satisfies in class GenericAssert<File>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual File does not satisfy the given condition.
      See Also:
    • doesNotSatisfy

      public FileAssert doesNotSatisfy(Condition<File> condition)
      Verifies that the actual File does not satisfy the given condition.
      Specified by:
      doesNotSatisfy in class GenericAssert<File>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File satisfies the given condition.
      NullPointerException - if the given condition is null.
      See Also:
    • is

      public FileAssert is(Condition<File> condition)
      Verifies that the actual File satisfies the given condition.
      Specified by:
      is in class GenericAssert<File>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual File does not satisfy the given condition.
    • isNot

      public FileAssert isNot(Condition<File> condition)
      Verifies that the actual File does not satisfy the given condition.
      Specified by:
      isNot in class GenericAssert<File>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File satisfies the given condition.
      NullPointerException - if the given condition is null.
    • hasSameContentAs

      public FileAssert hasSameContentAs(File expected)
      Verifies that the content of the actual File is equal to the content of the given one. Adapted from FileAssert (from JUnit-addons.)
      Parameters:
      expected - the given File to compare the actual File to.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the file to compare to is null.
      AssertionError - if the the actual File is null.
      AssertionError - if the content of the actual File is not equal to the content of the given one.
    • fail

      private void fail(File expected, FileContentComparator.LineDiff[] diffs)
    • cannotCompareToExpectedFile

      private void cannotCompareToExpectedFile(File expected, Exception e)
    • assertExists

      private FileAssert assertExists(File file)
    • isRelative

      public FileAssert isRelative()
      Verifies that the actual File is a relative path.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is not a relative path.
    • isAbsolute

      public FileAssert isAbsolute()
      Verifies that the actual File is an absolute path.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual File is not an absolute path.
    • overridingErrorMessage

      public FileAssert overridingErrorMessage(String message)
      Replaces the default message displayed in case of a failure with the given one.

      For example, the following assertion:

       assertThat("Hello").isEqualTo("Bye");
       
      will fail with the default message "expected:<'[Bye]'> but was:<'[Hello]'>."

      We can replace this message with our own:

       assertThat("Hello").overridingErrorMessage("'Hello' should be equal to 'Bye'").isEqualTo("Bye");
       
      in this case, the assertion will fail showing the message "'Hello' should be equal to 'Bye'".

      Specified by:
      overridingErrorMessage in class GenericAssert<File>
      Parameters:
      message - the given error message, which will replace the default one.
      Returns:
      this assertion.