Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Showdown of the Asserts

Showdown of the Asserts

This talk is based on heated discussions about the syntax of asserts. Do you like the good old assertNotEquals(id, c.getId()) or do you prefer something fancier like assertThat(c.getId(), is(not(equalTo(id))))?
We will take a quick look at the differences between asserts in JUnit, TestNG, Hamcrest, AssertJ, Truth, and Spock. This topic is at least as important as Vim vs Emacs and tabs vs spaces!

Philipp Krenn

October 01, 2015
Tweet

More Decks by Philipp Krenn

Other Decks in Programming

Transcript

  1. JUnit public class AdderTest { @Test public void testAdd(){ Adder

    adder = new Adder(); assertEquals(3, adder.add(1, 2)); } }
  2. // JUnit assertEquals(3, adder.add(1, 2)); // TestNG assertEquals(adder.add(1, 2), 3);

    // Hamcrest assertThat(adder.add(1, 2), equalTo(3)); // AssertJ assertThat(adder.add(1, 2)).isEqualTo(3); // Truth assertThat(adder.add(1, 2)).isEqualTo(3); // Spock expect: adder.add(1, 2) == 3
  3. JUnit final Double MY_PI = 3.14; assertEquals( "My own pi

    is close to the real pi", Math.PI, MY_PI, 0.1)
  4. // JUnit assertEquals(Math.PI, MY_PI, 0.1) // Hamcrest assertThat(MY_PI, closeTo(Math.PI, 0.1));

    // AssertJ assertThat(MY_PI).isCloseTo(Math.PI, Percentage.withPercentage(3)); // Truth assertThat(MY_PI).isWithin(0.1).of(Math.PI); // Spock with Hamcrest matcher expect: MY_PI closeTo(Math.PI, 0.1)
  5. “Want to get much better at writing unit tests? For

    the next week, try writing every single assertion using equal()” https://medium.com/javascript-scene/what-every-unit-test- needs-f6cd34d9836d