JUnit Snippets

While investigating something completely different I visited the JUnit4 homepage and found this snippet in the banner:

@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"))
    )
  ));
}

Lately(TM) I produced the following code (in ignorance of this nice fluent assertions):

@Test
public void shouldAddBuildConfigurationEnvironmentVariables() {
  ...
  assertThat( actual.getEnvironmentVariables().size() ).isEqualTo( 2 );
  List<String> keys = actual.getEnvironmentVariables()
    .stream()
    .map( EnvironmentVariable::getKey )
    .collect( Collectors.toList() );
  List<String> values = actual.getEnvironmentVariables()
    .stream()
    .map( EnvironmentVariable::getValue )
    .collect( Collectors.toList() );

  assertThat( keys ).contains( "foo" );
  assertThat( values ).contains( "bar" );
  assertThat( keys ).contains( "baz" );
  assertThat( values ).contains( "test" );
}

What a shame...

Conditionally skipping a test

import static org.junit.Assume.assumeThat;


@Test
public void defaultAndLocalizedOutput() {
    // TODO - this tests doesn't work on my developer Mac
    assumeThat(getProperty("os.name"), is(not("Mac OS X")));

}

Other test related posts on this blog: