Unit Testing, Continuous Integration, and Refactoring
(for your own examination - not for submission)
The following sections will give you introductory examples on:
Test Automation and JUnit
You can put test automation into practice using this codebase for a demo application: unit-testing-demoapp.zip.
The project has three Java model classes (none of the classes has the main
method because we are not dealing with the independently executable application). The project consists of only the following model classes:
SaleItem
- Can be used for different products. Each product has a name and unit price.Bill
- One bill record. Consists of one more rows. Can calculate the total sum of the rows.BillRow
- One row in the bill. Has a link to the corresponding product (SaleItem) and sold quantity number. Can have also discount percentage for the concrete sale and product (by default discount percentage is 0%). Can calculate row total price.
Application code is located in the \src\main\java
folder. In addition, the project includes also the automatic unit tests created using the JUnit framework. The code of the tests is located in the \src\test\java
folder. Tests can be executed with the gradle task test
or the IDE built-in tools.
For training: Test classes have many empty test methods that should check different model properties and functionality. Try to develop these tests.
Note! One of the classes has several mistakes in error handling. Can you find them with tests?
Continuous Integration (CI)
While developing your application you continuously write code and test it. It is assumed that you re-run your test suits every time the new change is introduced. You, as a good developer, may consider pressing the same button over and over again as tedious work, and it definitely should be automated. Such a technique exists and called Continuous Integration (CI). Also, CI helps to overcome the "it has been working on my machine ¯\_(ツ)_/¯" problem. After each push to the repository, CI automatically build and test your application and notify you of the status.
Bitbucket pipelines allow us to add a simple CI workflow by just committing a file named bitbucket-pipelines.yml
in the root of your project. The file should look as follow:
image: openjdk:11 pipelines: default: - step: script: - bash ./gradlew build test
Refactoring
The code base for the demo application is here: refactoring-demoapp.zip.
The application is reading data about doctors and patients from the CSV files located in the resources folder. After reading the files, the application creates corresponding objects and prints them to the console. However, the application code is far from perfection and has many problems, especially related to the code duplication. You need to refactor the code and make it better. Hint: You can use inheritance/generalization and split/combine methods. Also, obvious bugs in exception handling should be fixed - only catch exceptions you can reasonable handle (printStackTrace or logging is almost never enough) and close streams in finally blocks.