14. Unit testing
Main steps
- Set up environment
- Set up tested classes
- Creation of new JUnit test case
- Manual testing
- Automated testing
- Automated testing using modified Test Suite
- Optional task
- More info
Set up environment
- Investigate jtm.activity14 package
- Install EclEmma and check test coverage
Set up tested classes
- Implement class Student similarly to Teacher class
- 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
- In Package Explorer right-click on src/test/java folder and create jtm.activity14 package.
- Right-click on created jtm.activity14 package and select: New— New JUnit Test Case.
- In New JUnit Test case window:
- Check that test version is: New JUnit 4 test,
- Check that test location is: .../src/test/java,
- Check that package is: jtm.activity14,
- Set test Name: DatabaseUnitTest1 (this name important, as it will be used by automated test of test)
- If necessary, mark setUpBeforeClass(), tearDownAfterClass(),
- If necessary, mark setUp() and tearDown(),
- If necessary, mark GenerateComments,
- Set Class under test: jtm.activity14.StudentManager,
- Press Next.
- In Test methods window:
- Mark StudentManager class, to test all its methods,
- If necessary, mark Create final method stubs,
- If necessary, ark Create tasks for generated tests methods,
- Press Finish.
Manual testing
- Select: Run— Run as— JUnit test, to test this file as simple unit test.
- 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.
- Create public static void main(String[] args) method, to allow test run as Java application.
Create 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 coverage status like:
...
*** Total test coverage is: xx% ***where xx is actual coverage of tested package.
Automated testing using modified Test Suite
- Open AllTests.java class in jtm.testSuite package,
- If necessary, comment out unneeded classes from ..allTests() {printStat( list,..
- Run Test suite.
Optional task
Select Help — Eclipse Marketplace..., for name enter jd and press Install for Enhanced Class Decompiler plugin.
or- Install and set up JD-Eclipse decompiler.
- 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).