07.01.2013 Views

Lecture Notes in Computer Science 3472

Lecture Notes in Computer Science 3472

Lecture Notes in Computer Science 3472

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

502 Zhen Ru Dai<br />

TestResult<br />

TestCase<br />

run(TestResult)<br />

runTest()<br />

setUp()<br />

tearDown()<br />

fName<br />

runTest()<br />

Test<br />

run(TestResult)<br />

Fig. 17.2. JUnit Classes<br />

TestSuite<br />

run(TestResult)<br />

addTest(Test)<br />

unexpected way. If one does not have an automated test, broken subsystems<br />

may not be tested. For these reasons, unit-level tests assure that the code a<br />

developer has written will be tested automatically, and thus give confidence <strong>in</strong><br />

the code.<br />

JUnit is a framework for writ<strong>in</strong>g unit tests. This section <strong>in</strong>troduces the JUnit<br />

framework and provides the mapp<strong>in</strong>g from the U2TP concepts to JUnit concepts.<br />

Fundamentals of JUnit JUnit is written by Erich Gamma and Kent Beck. It is an<br />

open source framework for automated unit tests. JUnit tests are based on Java.<br />

Thus, its execution code is also Java for which there exist various compilers.<br />

By means of the JUnit framework, software developers can derive tests from<br />

classes of the JUnit Framework and comb<strong>in</strong>e them <strong>in</strong>to a test suite. A unit test<br />

checks a s<strong>in</strong>gle unit of the implementation code, e.g. a method or a class. Unit<br />

tests are applied <strong>in</strong> a test driven design. The tests are developed <strong>in</strong> parallel to<br />

code implementation. In the former, tests are even written before the codes are<br />

implemented. The software developer writes the test rout<strong>in</strong>es for every piece of<br />

code so that the test can be re-run every time the code is changed. For eXtreme<br />

Programm<strong>in</strong>g [XP], where code <strong>in</strong>tegration and code test<strong>in</strong>g are required permanently,<br />

unit tests are obligatory. Because of its simplicity, JUnit has become<br />

very popular <strong>in</strong> the XP doma<strong>in</strong>. JUnit is <strong>in</strong>tegrated <strong>in</strong> many toolkits, e.g. Eclipse<br />

[ECla] or JBuilder [JBu].<br />

In JUnit, a test case is the basic abstraction. A test case can be run <strong>in</strong> order<br />

to check if the test passes. It conta<strong>in</strong>s the test methods setUp() and tearDown(),<br />

a ma<strong>in</strong>() method which runs the test case and an optional suite() method which<br />

groups test methods <strong>in</strong> a test suite (Figure 17.2). Usually, every class that is<br />

written should have a test case. A JUnit test suite is structured <strong>in</strong> a hierarchical<br />

way and is composed of test cases or other test suites. A test fixture allows test

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!