Dynamic tests
As we know, in JUnit 3, we identified tests by parsing method names and checking whether they started with the word test. Then, in JUnit 4, we identified tests by collecting methods annotated with @Test
. Both of these techniques share the same approach: tests are defined at compile time. This concept is what we call static testing.
Static tests are considered a limited approach, especially for the common scenario in which the same test is supposed to be executed for a variety of input data. In JUnit 4, this limitation was addressed in several ways. A very simple solution to the problem is to loop the input test data and exercising the same test logic (JUnit 4 example here). Following this approach, one test is executed until the first assertion fails:
package io.github.bonigarcia; import org.junit.Test; public class MyTest { @Test public void test() { String[] input = { "A", "B", "C" }; for (String s : input) { exercise(s); } } ...