DEV Community

Genne23v
Genne23v

Posted on

Adding Unit Testing to OpenSSG

I had felt the need to package my OpenSSG like node.js modules. I should have done packaging at the beginning of the project due to lack of my Java knowledge. I had delayed to find a way to package my app, but I tried to use Gradle. There are other ways to package Java command line app. I decided Gradle because one of repos that I found uses Gradle.

❯ gradle test                                                                                     ─╯

BUILD SUCCESSFUL in 723ms
4 actionable tasks: 3 executed, 1 up-to-date
Enter fullscreen mode Exit fullscreen mode

I can run the test quickly with Gradle now

I chose JUnit for my unit testing since it's the most popular Java testing framework. So I believe that learning JUnit will be great asset for me. And I had a bit of exposure from writing unit tests for Crowdin CLI project before. I know writing good test requires a lot of thinking and skills, however, JUnit assertion methods are less and simpler than Javascript assertions.

I think writing unit tests is another important step to review my code to improve the logic and split the functions whenever necessary for better readability and easier modification. Since I did some refactoring work to split my code into much smaller chunks, writing unit test is not much difficult. But I found some errors while writing tests. One of the errors was in getUrl(). getUrl() is a recursive function that each child node calls parent node to complete file path. I realized that the root node is not add its data. So the link in sidebar menu has never been correct. And it didn't even add file extension after returning file path. And I updated DomNode copy constructor as per SpotBugs suggestion to deep copy the object. I had a doubt that it shouldn't be a deep copy in this case, and my unit test proves that I shouldn't have done that. And I also found that setParent() did not add the child to its parent. So the child knows the parent, the parent didn't.

There are many bugs and edge cases while writing my tests, but I leave them for now as I need a major update to my argument validation logic as its complexity is too high right now. Moreover, it's almost impossible to read the logic, even myself. And I have some errors that are not handled properly. What's good about unit test, when I make a change, it can find things that break from my change. So I can prevent another bugs from my change. I had some test writing experiences in a co-op and open source projects. I couldn't understand why we write simple tests all over. Test didn't seem to do important work. But now I know the meaning of meme that the guy was disappointed by the co-worker who procrastinates writing unit tests. Especially, it's more important when the code base is large that hundreds of developers are working on. Unit test makes developers find the breaks early enough. And release process much faster with robust testing.

Top comments (0)