-1

I've found only Hamcrest assertions in Hamcrest project.

Tests in the applications I saw are run with JUnit runner.

Is there a way to run tests with Hamcrest or is Hamcrest not for this?

glytching
  • 44,936
  • 9
  • 114
  • 120
Diamond
  • 217
  • 1
  • 3
  • 7

1 Answers1

3

Hamcrest is not a testing framework, it is an assertions library which tries to make tests more readable.

JUnit 4 (and Hamcrest itself) provides an assertThat() method which accepts a Hamcrest matcher, for example:

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;

public class ATest {
    @Test
    public void someTest() {
        String expected = "...";

        String actual = doSomething();

        assertThat(actual, is(expected));

    }
}

So, a typical usage pattern is:

  • Run your tests with a JUnit runner
  • Use Hamcrest's matchers to assert the success (or otherwise) of your tests

More details on Hamcrest here and on its use alongside JUnit here.

glytching
  • 44,936
  • 9
  • 114
  • 120
  • Thanks, and is it hamcrest? "Hamcrest matchers for testing Java beans", but not hamcrest package "com.google.code.bean-matchers". https://mvnrepository.com/artifact/com.google.code.bean-matchers/bean-matchers/0.11 – Diamond Feb 19 '18 at 17:48
  • I'm not sure what you are asking in that last comment. This: "com.google.code.bean-matchers" was not mentioned in the original question so I can't work out what you are trying to ask in your last comment. – glytching Feb 19 '18 at 17:52