It is a good practice to have test cases covering major portion of actual code base. The idea was same to add new test cases in Loklak Server to increase its test coverage. The results were quite amazing with a significant increase of about 3% in total test coverage of the overall project. And about 80-100% increase in the test coverage of individual files for which tests have been written.
For integration, a total of 6 new test cases have been written:
For increasing code coverage, Java docs have been written which increase the lines of code being covered.
Basic implementation of adding a new test case is same. Here’s is an example of EventBriteCrawlerServiceTest implementation. This can be used as a reference for adding a new test case in the project.
Prerequisite: If the test file being written tests any additional external service (e.g. EventBriteCrawlerServiceTest tests any event being created on EventBrite platform) then, a corresponding new test service or test event should be written beforehand on the given platform. For EventBriteCrawlerServiceTest, a test-event has been created.
The given below steps will be followed for creating a new test case (EventBriteCrawlerServiceTest), assuming the prerequisite step has been done:
- A new java file is generated in test/org/loklak/api/search/ as EventBriteCrawlerServiceTest.java.
- Define package for the test file and import EventBriteCrawlerService.java file which has to be tested along with necessary packages and methods.
- Define the class and methods that need to be tested. It is a good idea to break the testing into several test methods rather than testing more features in a single method.
- Create an object of EventBriteCrawlerService in each test method.
- Call specific method of EventBriteCrawlerService that needs to be tested in each of the defined methods of test file and assert the actualresult to the expectedresult.
- For methods fetching an actual result (Integration tests) of event page, define and initialise an expected set of results and assert the expected result with the actual result by parsing the json result.
- If the test file is testing the harvester, then import and add the test class in TestRunner.java file. e.g.
The last step will be to check if the test case passes with integration of other tests. The gradle will test the test case along with other tests in the project.