JUnit 4
JUnit is the most popular unit test framework for Java, and it is shipped with Eclipse Java Development Tools (JDT). In particular, the examples in this book are based on JUnit version 4.
To implement JUnit tests, you just need to write a class with methods annotated with @org.junit.Test
. We will call such methods simply test methods. Such Java or Xtend classes can then be executed in Eclipse using the JUnit test launch configuration. All methods annotated with @Test
will be then executed by JUnit. In test methods you can use assert methods provided by the org.junit.Assert
class. For example, assertEquals(expected, actual)
checks whether the two arguments are equal; assertTrue(expression)
checks whether the passed expression evaluates to true
. If an assertion fails, JUnit will record such failure. In Eclipse, the JUnit view will provide you with a report about tests that failed. Ideally, no test should fail, and you should see the green bar in the JUnit view.
Tip
All test methods can...