DEV Community


Posted on

Unit testing using JUNIT

As a weekly course work, I added JUNIT testing framework into my ssg application.

Adding framework

Junit is a most popular testing framework that is being used for Java developers.
I was able to add Junit API and frameworks using maven dependencies and it was very quick and easy.
After add the dependencies, I was able to create test class from the class that I want to test.

About Unit testing

It was my first time creating testing methods for unit tests.
Unit tests are created based on assert methods that are comparing the expected value and actual value.

Wrote first unit test for the simplest method

Below is my first unit testing method, which was to test the function for converting the filename from array of strings to a string.

void testFileNameReader() {
        String[] fileName1 = {"example", "file", "name.txt"};
        assertEquals("example file name.txt",HTMLBuilder.fileNameReader(fileName1));

        String[] fileName2 = {""};
Enter fullscreen mode Exit fullscreen mode

Core unit testing

As one of the core unit testing, I chose a method in "" which is created to parse text or md file and write a html file, which was created at the beginning of the project.
To create a unit testing, I had to create a mock sample file to compare which contain the content that is expected to be created.
I used a text file for the comparison and used a method "assertTrue".
Since the compared object is a file, it was easier with using FileUtils library and compare the two files.

For contributors, I updated the documentation for the testings, explaining how it works and how to use it.
In that case, it would be easier to review and merge codes for me, and easier for the contributors to find out any bugs or improvements from the code.

You can click here to see the changes I made through multiple commits.

Top comments (0)