junit.org

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

JUnit 4 is in maintenance mode.
At this point, only critical bugs and security issues will be fixed.
All other issues and PRs will therefore be declined.
For ongoing development, please check out the junit-framework repository.

    @Test
    public void newArrayListsHaveNoElements() {
        assertThat(new ArrayList<Integer>().size(), is(0));
    }
    @Test
    public void sizeReturnsNumberOfElements() {
        List<Object> instance = new ArrayList<Object>();
        instance.add(new Object());
        instance.add(new Object());
        assertThat(instance.size(), is(2));
    }
              

Annotations

Start by marking your tests with @Test .

    @Test
    public void lookupEmailAddresses() {
        assertThat(new CartoonCharacterEmailLookupService().getResults("looney"), allOf(
            not(empty()),
            containsInAnyOrder(
                allOf(instanceOf(Map.class), hasEntry("id", "56"), hasEntry("email", "roadrunner@fast.org")),
                allOf(instanceOf(Map.class), hasEntry("id", "76"), hasEntry("email", "wiley@acme.com"))
            )
        ));
    }
              

Hamcrest matchers

Make your assertions more expressive and get better failure reports in return.

Third-party extensions

Read the original on junit.org ↗