First contribution to Java project
I wanted to try Java project to experience more professional code in Java. I picked up Crowdin CLI as it seems either not too big or not too small. The issue was asking to write a test for newly introduced feature.
What is Crowdin CLI?
Initially I tried to understand what Crowdin CLI for better understanding when I look at the code base. Crowdin provides the location service for many languages. Crowdin CLI is a client tool that you can upload translations and download the completed work automatically. Users can also find out the translation work status.
My approach to write test
After some of trials in my terminal, I looked at the code that I need to write a test against. It's DownloadSourcesAction.java
which downloads translation based on options provided by the user. Some tests were already written, but obviously they don't cover many branches of the function. I tried to understand the test and write my test in a similar manner. Unfortunately, I don't have much room to customize passing arguments as it's a client tool. I could change limited parameters to hit the branches that the existing tests don't hit.
Then I tried to understand the logic to see how I can vary my parameters to create a different test case. Good thing was that I had only one function to test as the rest functions are private, what was difficult for me is that the main function is very long and it's hard to read how each block of code is related to other. So I started to test with different parameters to have a better understanding. What I realized later is that my test can't verify the same download action. Because my test only needs to download reviewed files, but I don't have a way to change the file status. And I searched the whole project if there is any property or method to update the review status, but I found that I can only hit the API for reviewed files. So I changed my strategy to verify the console message when I set a different parameters. I invoke the action with different parameters, then it shows the message on the console.
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream ps = System.out;
System.setOut(new PrintStream(outContent));
...
String warnMessage = "⚠️ Operation is available only for Crowdin Enterprise\n";
assertEquals(warnMessage, outContent.toString());
This is how to capture console message and assert
I pushed this test to the repo and I could see the test coverage is increased. The other test testDryRun()
seemed not effective, so I updated the test to verify console message too.
So much learning about how to package Java program
Besides from writing tests, I learned many things from this project as I'm writing Java CLI tool too. I packaged my OpenSSG with Gradle and learned how to manage dependencies and settings, as well as CI tools. As I mentioned in the earlier blog, learning various tools and practises from real world project is tremendous.
Top comments (2)
thank you for the contribution!
It is my pleasure and I really appreciate you read my blog. I would love to make more contributions to Crowdin!