Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 914 Bytes

UseCorrectAssertInTests.md

File metadata and controls

36 lines (28 loc) · 914 Bytes

Java assert statements are not run unless explicitly enabled via runtime flags to the JVM invocation.

If asserts are not enabled, then a test using assert would continue to pass even if a bug is introduced since these statements will not be executed. To avoid this, use one of the assertion libraries that are always enabled, such as JUnit's org.junit.Assert or Google's Truth library. These will also produce richer contextual failure diagnostics to aid and accelerate debugging.

Don't do this:

@Test
public void testArray() {
  String[] arr = getArray();

  assert arr != null;
  assert arr.length == 1;
  assert arr[0].equals("hello");
}

Do this instead:

import static com.google.common.truth.Truth.assertThat;

@Test
public void testArray() {
  String[] arr = getArray();

  assertThat(arr).isNotNull();
  assertThat(arr).hasLength(1);
  assertThat(arr[0]).isEqualTo("hello");
}