JUNIT JUnit ( http://www.junit.org ) is a framework for writing tests in Java, written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of Extreme Programming methodology). It has become the unof�cial standard for Java testing Supported by many IDEs Widely imitated: C++, Perl, Python, .NET all have versions Once you know one, you can easily use others 5 / 7 1 / 7
USING JUNIT JUnit uses re�ection to look up methods Re�ection: extracting characteristics of a class or method at run time. Pass in the class containing your tests JUnit decides which methods to run based on their annotations An annotation is syntactically like a type. If a method is annotated with “@Test”, TestRunner will execute it 5 / 7 2 / 7
JUNIT STRUCTURE 5 / 7 3 / 7
EXECUTION FLOW Each method in a Unit Test can be labelled with @Before, @Test, @After 1 / 7 4 / 7
Each Unit Test has many test cases, and for each test case: @Before: The setup phase is in a (single) method annotated with @Before. Here, you do some setup/prep work before running the tests, e.g., open a �le, open a network connection. @Test: Run the test case. @After: The teardown phase is in a single method annotated with @After. Here, you clean up after each test �nishes, e.g., closing the �le, close the connection. Methods annotated with @BeforeClass and @AfterClass run once overall. 2 / 7 5 / 7
FYI It’s BTA, BTA, BTA, ... , not BTTTTTTTA. That is, these run before and after every test case. 3 / 7 6 / 7
DEMO TIME Demo of JUnit with Eclipse: https://www.youtube.com/watch?v=I8XXfgF9GSc 4 / 7 7 / 7
Recommend
More recommend