Java Code Examples for org.hamcrest.MatcherAssert

Following code examples demonstrate how to use org.hamcrest.MatcherAssertfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.hamcrest.MatcherAssertand various code implementation of this class.

    public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, actual, matcher);
        } catch (AssertionError e) {
            log.error(e.getMessage());
            throw new AssertionError(e);
        }
    } 


    public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
        try {
            org.hamcrest.MatcherAssert.assertThat("", actual, matcher);
        } catch (AssertionError e) {
            log.error(e.getMessage());
            throw new AssertionError(e);
        }
    } 

    public static void assertThat(String reason, boolean assertion) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, assertion);
        } catch (AssertionError e) {
            log.error(e.getMessage());
            throw new AssertionError(e);
        }
    } 

    public static void assertThat(String reason, boolean assertion) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, assertion);
        } catch (AssertionError ae) {
            LOGGER.error("*****Custom Assertion Error" + reason + "*****\n" + ae.toString());
            ScreenShotUtils.saveScreenShotAs(reason, reason);
            throw ae;
        }
    } 

    public static void verifyThat(String reason, boolean assertion) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, assertion);
        } catch (AssertionError ae) {
            LOGGER.error("*****Custom Assertion Error" + reason + "*****\n" + ae.toString());
            ScreenShotUtils.saveScreenShotAs(reason, reason);
            String verificationFailureMessage = "\nVerification Failure";
            verificationFailureMessage += "\t" + reason + "\n\t\tExpected: " + !assertion + "\n\t\t  Actual: "
                    + assertion;
            verificationFailureMessage += "\nException: " + ae.toString();
            addVerificationFailure(verificationFailureMessage);
        }
    } 

    public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, actual, matcher);
        } catch (AssertionError ae) {
            LOGGER.error("*****Custom Assertion Error" + reason + "*****\n" + ae.toString());
            ScreenShotUtils.saveScreenShotAs(reason, reason);
            throw ae;
        }
    } 

    public static <T> void verifyThat(String reason, T actual, Matcher<? super T> matcher) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, actual, matcher);
        } catch (AssertionError ae) {
            LOGGER.error("*****Custom Assertion Error" + reason + "*****\n" + ae.toString());
            ScreenShotUtils.saveScreenShotAs(reason, reason);
            String verificationFailureMessage = "\nVerification Failure";
            verificationFailureMessage += "\nException: " + ae.toString();
            addVerificationFailure(verificationFailureMessage);
        }
    } 

    private void test_that_an_invoice_has_all_zero_value(Invoice invoice){
        assertNotNull(invoice);
        org.hamcrest.MatcherAssert.assertThat(invoice.getTaxIncludedPrice(), is(0));
        org.hamcrest.MatcherAssert.assertThat(invoice.getVAT(), is (0));
        org.hamcrest.MatcherAssert.assertThat(invoice.getTaxExcludedPrice(), is(0));
        org.hamcrest.MatcherAssert.assertThat(invoice.getVatRate(), is (0.0));
    } 

  protected void assertEquals(String message, Object expected, Object actual) {
    MatcherAssert.assertThat(message, actual, Matchers.equalTo(expected));

  } 

  @Test
  public void copyDirectoryFromRunfilesWithoutPrefixShouldCopyTheWholeDirectory()
      throws IOException {
    String knownFile = "build_bazel_integration_testing/javatests/build/bazel/tests/integration/WorkspaceDriverTest/" + jarNameAccordingToCurrentBazelVersion();

    driver.copyDirectoryFromRunfiles("build_bazel_integration_testing/javatests/build/bazel/tests/integration", "");

    Optional<Path> actualFilePath = findPath(driver.workspaceDirectoryContents(), knownFile);
    org.hamcrest.MatcherAssert.assertThat(
        "the known file should be found in the workspace", actualFilePath, is(optionalWithValue()));
  } 

  public void scratchFileShouldCreateFileAndWorkspaceContentsContainThatFile() throws IOException {
    String content = "somecontent";
    String path = "somePath";

    driver.scratchFile(path, content);

    Optional<String> actualScratchFileContent =
        findPath(driver.workspaceDirectoryContents(), path).map(this::readFileContent);
    org.hamcrest.MatcherAssert.assertThat(
        actualScratchFileContent, is(optionalWithValue(equalTo(content))));
  } 

  @Test
  public void copyDirectoryFromRunfilesShouldCopyTheWholeDirectoryAndStripPrefix()
      throws IOException {
    String sourceDirectoryThatWillBeStripped = "build_bazel_integration_testing/javatests/build/bazel/tests/integration";
    String theFileToCopy = "/WorkspaceDriverTest/" + jarNameAccordingToCurrentBazelVersion();
    String knownFile = sourceDirectoryThatWillBeStripped + theFileToCopy;

    driver.copyDirectoryFromRunfiles(
        sourceDirectoryThatWillBeStripped, sourceDirectoryThatWillBeStripped);

    Optional<Path> actualFilePath = findPath(driver.workspaceDirectoryContents(), theFileToCopy);
    org.hamcrest.MatcherAssert.assertThat(
        "the known file should be found in the workspace", actualFilePath, is(optionalWithValue()));
    org.hamcrest.MatcherAssert.assertThat(
        "the root path from the runfiles should be stripped",
        actualFilePath.map(Path::toString),
        is(not(optionalWithValue(endsWith(knownFile)))));
  } 

  public void scratchExecutableFileShouldCreateAnExecutableFile() throws IOException {
    String path = "someExecutablePath";

    driver.scratchExecutableFile(path);

    Optional<Boolean> isExecutable =
        findPath(driver.workspaceDirectoryContents(), path).map(this::isExecutable);
    org.hamcrest.MatcherAssert.assertThat(
        "The file should be executable", isExecutable, is(optionalWithValue(equalTo(true))));
  } 

  public void fileExistsUnderWorkspacePath() throws IOException {
    driver.scratchFile("test_workspace_file");
    org.hamcrest.MatcherAssert.assertThat(
        "test_workspace_file should be in the workspace",
        new File(driver.currentWorkspace().toFile(), "test_workspace_file").exists(),
        is(true));
  } 

  public void newWorkspaceCreatesANewCleanWorkspace() throws IOException {
    String path = "somePathForNewWorkspace";
    driver.scratchFile(path);

    driver.newWorkspace();

    Optional<Path> fullPath = findPath(driver.workspaceDirectoryContents(), path);
    org.hamcrest.MatcherAssert.assertThat(
        "Workspace should be cleaned", fullPath, is(emptyOptional()));
  } 

  public void differentAssertions() {
    org.hamcrest.MatcherAssert.assertThat(1, Matchers.is(1));
    org.hamcrest.MatcherAssert.assertThat("reason", 1, Matchers.is(1));
  } 

    public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, actual, matcher);
        } catch (AssertionError e) {
            log.error(e.getMessage());
        }
    } 

    public static void assertThat(String reason, boolean assertion) {
        try {
            org.hamcrest.MatcherAssert.assertThat(reason, assertion);
        } catch (AssertionError e) {
            log.error(e.getMessage());
        }
    } 

    public static <T> void verifyThat(T actual, Matcher<? super T> matcher) {
        try {
            org.hamcrest.MatcherAssert.assertThat("", actual, matcher);
        } catch (AssertionError e) {
            log.error(e.getMessage());
        }
    } 

Advertisement
Advertisement