[OSD600] Testing

Trang Nguyen
2 min readNov 10, 2021

Introduction

For this week in open source development, we were asked to implement testing for our static site generators. We had to research the optimum testing platform for our codebase. Since my static site generator uses Java, I opted to use JUnit for my unit testing and integration testing.
The reason I chose JUnit is that it is one of the most popular Java testing platforms and allows for very in-depth test creations. I also really liked that Intellij could utilize it, which made my implementations that much easier.

Implementing Testing Code

Since my static site generator has many helper functions that allow me to process folders, files, write to files, create CSS files, etc, I began my quest of writing my tests.

Since my code works with files, my approach was to call the functions, let them produce the expected file, and verify by using assert the command. The assert command allows us to verify the expectation of the return of a function.

For my unit tests, my approach would be to follow the BDD model (Given — When — Then) to cover all of the aspects of the function. I also write tests for expected exceptions. My exception unit test in the MDUtilsclass would be like this:

@Test
public void shouldThrowsExceptionFromNullPath(){
// GIVEN
String path = null;

// WHEN
AbstractThrowableAssert throwable = assertThatThrownBy(()->utils.convertMdToHTML(path));

// THEN
throwable.isInstanceOf(NullPointerException.class);

}

And I use integration tests for HTMLProcessor.java to verify the whole function works well.

@Test
void shouldCreateNewFileFromValidPathAndValidOutputPathAndValidLanguage() throws IOException {
String inputPath = TestUtils.generateRandomInputPath();

String outputPath = TestUtils.generateOutputPath();

String language = "en";

// WHEN
processor.convertToHTML(inputPath, outputPath, language);

// THEN
Path folderPath = Files.list(Paths.get("./src/main/resources/output")).findFirst().get();

assertThat(folderPath).exists().toString().endsWith(".html");

// clean directory after the test
utils.cleanDirectory(Paths.get(outputPath));
}

To view all of my tests, click on this commit 4b18e4b

Implementing test coverage

In Intellij, to view the test coverage, create a folder in resources where you can store your report. Then right click on the root folder of test -> `More Run/Debug -> Run Tests in com.os.jssg with Coverage -> Click on Generate Coverage Report on the left side of popup modal

Conclusion

Overall, I had a lot of fun implementing testing code via JUnit. I believe that with the addition of new functions, I will be able to quickly create effective tests that will ensure my code is up to the full standards that I want to achieve.

--

--

Trang Nguyen

Computer Programming Student @Seneca. Writing to share solutions and encourage my sister to write.