13. JDBC
15. GUI Application

14. exercise: Unit Testing



  1. Review presentation JUnit


  2. Investigate jtm.activity14 package
  3. Install EclEmma and check test coverage

Set up tested classes

  1. Implement class Student similarly to Teacher class
  2. Implement StudentManager class, similarly to TeacherManager class in activity13.
    2. You can simply copy-paste methods and make necessary changes.
    3. Note that you can introduce bugs here, as it can be that not all necessary changes are done correctly.

Creation of new JUnit test case

  1. Right-click on jtm.activity14 package in Package Explorer and select: New— New JUnit Test Case.
  2. In New JUnit Test case window:
    1. Check that test version is: New JUnit 4 test,
    2. Check that test location is: .../src/main/java,
    3. Check that package is: jtm.activity14,
    4. Set test Name: DatabaseUnitTest1 (this name important, as it will be used by automated test of test)
    5. Mark setUpBeforeClass(), tearDownAfterClass() if necessary,
    6. Mark setUp() and tearDown() if necessary,
    7. Mark GenerateComments if necessary,
    8. Set Class under test: jtm.activity14.StudentManager,
    9. Press Next.
  3. In Test methods window:
    1. Mark StudentManager class, to test all its methods,
    2. Mark Create final method stubs, if necessary,
    3. Mark Create tasks for generated tests methods, if necessary,
    4. Press Finish.

Manual testing

  1. Select: Run— Run as— JUnit test, to test this file as simple unit test.
  2. Right-click on test (or package) in Package explorer and select: Coverage as– JUnit test, to check coverage of the tested classes.

Automated testing

Automated testing and coverage reporting is performed by test, which is compiled in JTM.jar package.
To test, how it will work manually you can do following.

Using main() method of the test

  1. Create public static void main(String[] args) method, to allow test run as Java application.
  2. Create following body for this method:

    public static void main(String[] args) {
        DatabaseUnitTest dbUnitTest = new DatabaseUnitTest();
  1. Select: Run— Run as...— Java application, to check, how your test will be executed from the automated test suite

Using modified Test Suite

  1. Open class in jtm.testSuite package,
  2. If necessary, comment out unneeded classes from ..allTests() {printStat( list,..
  3. Run Test suite.

Optional task

Install and set up JD-Eclipse decompiler, and check, how Junit tests are implemented in JTM.jar library.

More info

13. JDBC
15. GUI Application
Izveidojis Valdis Vītoliņš 2017-01-03 10:07
Pēdējais mainījis Valdis Vītoliņš 2017-08-31 18:46
Xwiki Powered
Creative Commons Attribution 3.0 Unported License