17. Unit testing
Review the presentation JUnit
Tasks
- Create the classes to be tested
- Create a new JUnit test
- Manual testing
- Automated testing
- Automated testing using a modified Test Suite
- Optional task
- Additional information
Create the classes to be tested
- Explore the jtm.activity17 package
- Implement the Student class similar to the Teacher class in the activity13 package
- Implement the StudentManager class, similar to the TeacherManager class
- You can simply copy-paste the methods and make the necessary changes
- Be aware that you can introduce errors here, as it is possible that not all the necessary changes are made correctly.
Create a new JUnit test
- In Package Explorer, right-click on the src/test/java folder and create a package called jtm.activity17.
- Right-click on the created package jtm.activity17 and select: New— New JUnit Test Case
- In the New JUnit Test case window:
- Check that the test version is: New JUnit 4 test,
- Check that the test location is: .../src/test/java,
- Check that the package is: jtm.activity17,
- Set the test name: DatabaseUnitTest1 (this name is important because it will be used by the automatic test test)
- If necessary, check setUpBeforeClass(), tearDownAfterClass(),
- If necessary, check setUp() and tearDown(),
- If necessary, check GenerateComments,
- Set the Class to be tested: jtm.activity17.StudentManager,
- Press Next.
- In the Test methods window:
- Check the StudentManager class to test all its methods,
- If necessary, check Create final method stubs,
- If necessary, check Create tasks for generated test methods,
- Click Finish.
Manual testing
- Select: Run— Run as— JUnit test to test this file as a simple unit test.
- Right-click the test (or package) in the Package Explorer and select: Coverage as– JUnit test to check the coverage of the tested classes.
Automated testing
Automated testing using the test's main() method
Automated testing and coverage reporting is performed by the DatabaseUnitTest.java test, which is compiled into the JTM.jar package.
To test how it will work manually, you can do the following.
- Create a public static void main(String[] args) method to allow the test to run as a Java application.
Create the following body for this method:
public static void main(String[] args) {
DatabaseUnitTest dbUnitTest = new DatabaseUnitTest();
dbUnitTest.test();
}Select: Run— Run as...— Java application to check how your test will be executed from the automated test suite. You should see the coverage status, for example:
...
*** Total test coverage is: xx% ***where xx is the actual coverage of the package being tested.
Automated testing using a modified Test Suite
- Open the AllTests.java class in the jtm.testSuite package
- Comment out unnecessary steps if necessary
- Run the test suite
Optional task
- Install EclEmma and check the test overlay.
Select Help — Eclipse Marketplace..., name enter jd and press Install for the plugin Enhanced Class Decompiler.
or- Install and configure the JD-Eclipse decompiler
- Check how the Junit tests are implemented in the reference JTM.jar library (for example, jtm.activity13 as a (for your needs) test example in the package jtm.activity17)