If it is not explicitly told in following recipe, setting up services are described for Ubuntu 20.04 server, but applications are described for Xubuntu 20.04 workstation. If you use different Ubuntu version or Linux distribution, settings as well as content, names and places of configuration files may be different!
Got it.

14. Unit testing



  1. Review presentation JUnit

    Practical exercise

  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. In Package Explorer right-click on src/test/java folder and create jtm.activity14 package.
  2. Right-click on created jtm.activity14 package and select: New— New JUnit Test Case.
  3. In New JUnit Test case window:
    1. Check that test version is: New JUnit 4 test,
    2. Check that test location is: .../src/test/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. If necessary, mark setUpBeforeClass(), tearDownAfterClass(),
    6. If necessary, mark setUp() and tearDown(),
    7. If necessary, mark GenerateComments,
    8. Set Class under test: jtm.activity14.StudentManager,
    9. Press Next.
  4. In Test methods window:
    1. Mark StudentManager class, to test all its methods,
    2. If necessary, mark Create final method stubs,
    3. If necessary, ark Create tasks for generated tests methods,
    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 using main() method of the test

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

  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();
  3. Select: Run— Run as...— Java application, to check, how your test will be executed from the automated test suite. You should see coverage status like:

    *** Total test coverage is: xx% ***

    where xx is actual coverage of tested package.

Automated testing using modified Test Suite

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

Optional task

  1. Select Help — Eclipse Marketplace..., for name enter jd and press Install for Enhanced Class Decompiler plugin.

  2. Install and set up JD-Eclipse decompiler.
  3. Check, how Junit tests are implemented in referenced JTM.jar library (e.g. jtm.activity13 as an (overengineered for your needs) example of your test in jtm.activity14 package).

More info


Created by Valdis Vītoliņš on 2017-01-03 08:07
Last modified by Valdis Vītoliņš on 2021-08-05 12:39
Xwiki Powered
Creative Commons Attribution 3.0 Unported License