Generate them.
An introduction to property based testing.
JUnit Theories
The Problem
The Solution (applause) Future PlansTesting division
public class MyTest {
@Test
public void testDivision() {
assertEquals("Integer division", 5, 15 / 3);
assertEquals("Double division", 0.5, 1.0 / 2);
}
@Test(expected = ArithmeticException.class)
public void testDivisionException() {
// should fail
final int i = 1 / 0;
}
}
@RunWith(Theories.class)
public class MyTheoryTest {
@DataPoints
public static int[] data = { 1, 2, 3, 4, 5, 6 };
@Theory
public void divisionIsTheReversionOfMultiplication(int a, int b) {
assertEquals(a, a * b / b);
}
}
@RunWith(Theories.class)
public class MyGeneratorTest {
@Theory
public void divisionIsTheReversionOfMultiplication(
@ForAll int a,
@ForAll @InRange(min = "1") int b) {
assertEquals(a, a * (b / b));
}
@Theory
public void concatenationLength(
@ForAll String s1,
@ForAll String s2) {
assertEquals(s1.length() + s2.length(), (s1 + s2).length());
}
}
\begin{align} \frac{a}{b} &= \frac{a}{b}\tag{1} \\ \\ a &= \frac{a}{b} \cdot b\tag{2} \end{align}
New ideas pass through three periods:1) It can’t be done.2) It probably can be done, but it’s not worth doing.3) I knew it was a good idea all along!
-- Arthur C. Clarke