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(id, is(not(equalTo(c.getId()))))`?
We will take a quick look at the differences between asserts in JUnit, TestNG, Hamcrest, fest, AssertJ, and Spock. This topic is at least as important as Vim vs Emacs and tabs vs spaces!
Topic: Software engineering practices
Language: English
41. Hamcrest
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class Hamcrest {
@Test
public void testAdd(){
Adder adder = new Adder();
assertThat(adder.add(1, 2), equalTo(3));
}
}
42. AssertJ
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class Assertj {
@Test
public void testAdd(){
Adder adder = new Adder();
assertThat(adder.add(1, 2)).isEqualTo(3);
}
}
43. Truth
import org.junit.Test;
import static com.google.common.truth.Truth.assertThat;
public class Truth {
@Test
public void testAdd(){
Adder adder = new Adder();
assertThat(adder.add(1, 2)).isEqualTo(3);
}
}
44. Spock
class Spock extends spock.lang.Specification {
def "Add two numbers"() {
def adder = new Adder()
expect:
adder.add(1, 2) == 3
}
}
68. “Wantto getmuch
better atwriting unit
tests? Forthe next
week,trywriting every
singleassertion using
equal()”
https://medium.com/javascript-scene/what-every-unit-test-
needs-f6cd34d9836d