Today, I would like to share a quick productivity tip. As developers, we sometimes skip writing extensive tests, particularly when under time pressure. As much as tests are necessary, they can also be tedious to write. For example, due to learning a new testing framework or when dealing with intricate business logic. Furthermore, this task often involves a certain degree of repetitive boilerplate, which can be a considerate discouragement for some developers. In this post, we’ll explore how generative AI saves time and effort without compromising on quality of writing detailed tests.

This post aims at Java and Kotlin developers who use IntelliJ as their IDE. We’ll guide you through the process of automatically generating a test class using IntelliJ, complemented by the EasyCode plugin powered by ChatGPT.

Table of Contents

Setup

Install ChatGPT – EasyCode plugin from the Marketplace.

Keep it DRY

Don’t repeat yourself, let’s start there. Crafting tests by hand from scratch is tedious to say the least. Luckily, IntelliJ can automatically generate a test class for you.

Let’s suppose you want to test a simple calculator.

class Calculator {
    fun add(num1: Int, num2: Int): Int {
        return num1 + num2
    }

    fun subtract(num1: Int, num2: Int): Int {
        return num1 - num2
    }

    fun multiply(num1: Int, num2: Int): Int {
        return num1 * num2
    }

    fun divide(num1: Int, num2: Int): Double {
        return num1.toDouble() / num2.toDouble()
    }
}

To generate a test class (on Mac OS) press ⌘ + N, choose Test and pick the desired framework. I’m using Kotest.

The outcome is an empty test class.

An empty test class as a stepping stone for the next course of action.

Don’t Write Tests. Generate Them.

Next, we will utilize the ChatGPT – EasyCode plugin to generate a skeleton or basic structure of the actual tests. We’ll initiate this process with some minimal customization to streamline future usage.

Assuming you’re in the test class, press ⌘ + A to select all its content. Then, right-click to get a contextual menu and choose Custom Prompt.

Custom prompts save time in the future – don’t repeat yourself.

I’ve called my custom prompt /kotest, here is what it contains: “Generate unit tests based on kotest and junit5”

Generate your tests using a custom prompt.

Summary

A lot is happening in the space of generative AI and automation of development tasks. In this post, we looked at a simple way how to leverage one of the IntelliJ plugins, EasyCode, to generate a rich test suite. I showed you how to apply a minimalistic customization so that the future use is easier. These tools are quickly evolving and features like the one shown today will likely become readily available out of the box. Stay tuned for further updates on this exciting topic. I hope that today’s article enhanced your productivity as a developer. Feel free to share your thoughts, ideas, and experiences on making AI part of your daily routines.


Tomas Zezula

Hello! I'm a technology enthusiast with a knack for solving problems and a passion for making complex concepts accessible. My journey spans across software development, project management, and technical writing. I specialise in transforming rough sketches of ideas to fully launched products, all the while breaking down complex processes into understandable language. I believe a well-designed software development process is key to driving business growth. My focus as a leader and technical writer aims to bridge the tech-business divide, ensuring that intricate concepts are available and understandable to all. As a consultant, I'm eager to bring my versatile skills and extensive experience to help businesses navigate their software integration needs. Whether you're seeking bespoke software solutions, well-coordinated product launches, or easily digestible tech content, I'm here to make it happen. Ready to turn your vision into reality? Let's connect and explore the possibilities together.