Waldo sessions now support scripting! – Learn more
App Development

Kluent: What It Is and How to Use It

Pius Aboyi
Pius Aboyi
Kluent: What It Is and How to Use It
December 27, 2022
7
min read

Assertions are ways to check that a specific condition is true in your tests. Assertions play a significant role in automated testing. As a result, most testing frameworks offer some form of assertion tool out of the box. However, standalone assertion libraries like Kluent exist to offer more robust and user-friendly assertions functionalities. Kluent is an assertion library for Kotlin that takes advantage of several of Kotlin's modern programming features.

In this post, you'll learn what Kluent is and how to use it in your Kotlin tests.

It uses Kotlin features Infix-Notations and Extension Functions to provide an expressive and human-readable syntax

What Is Kluent?

Kluent is simply a Fluent Assertion library for Kotlin. It uses Kotlin features Infix-Notations and Extension Functions to provide an expressive and human-readable syntax. Ordinarily, Fluent Assertions make writing assertions feel and look like plain English. Kluent builds upon the same idea using Kotlin.

What Is Kluent Used For?

You can use Kluent in several ways. In this section, we'll look at a few ways you can use Kluent to perform assertions.

You can use Kluent to make your assertions easy for you and others to read. For example, you can write an assertion like assertEquals(expected, actual), but Kluent can write it as expected shouldBeEqualTo actual, which is obviously easier to read.

Another use for Kluent is verifying that results from a test meet specific expectations. This is one of the primary features of all assertion libraries.

In addition to using an assertion library to verify that the result from a method meets certain expected values, you can also check whether the result from a test intentionally fails to meet a specific value.

Another helpful feature of Kluent is that it makes it easy to do assertions on a list. For example, you can easily use Kluent to verify whether a specific value is contained in a list.

Advantages of Kluent

There are many advantages to using Kluent. For one, Kluent makes communication about each test clearer for you and your team. Thanks to its highly human-readable syntax, it is easier for developers to understand the intent of a test.

It's possible to extend the functionalities of Kluent so that it meets even more custom test needs. And you can use Kluent in a mix with other testing libraries and frameworks like AndroidX Test. It will integrate well with other test tools you may be using.

Disadvantages of Kluent

Like most things, though, there are a couple of drawbacks to Kluent. Using Kluent will mean adding a new and extra dependency to your project. Though the library is lightweight, you're making the assertion tool built into your choice test framework redundant by adding an extra assertion library.

It also requires some learning. Even though Kluent has very friendly syntax, just like every time one picks up a new tool or library, you may have to invest some time in learning things you may already be familiar with from your last assertion library.

How to Perform Tests Using Kluent

Now that we know what Kluent is and what you can do with it, let's walk through how to use it.

For this guide, we'll use Kluent to perform assertions in an Android project using Android Studio. You can follow along by creating a new project in Android Studio or using an existing project.

Step 1: Install Kluent

Kluent is available as two Gradle plugins. One is the core Kluent and the second one is Kluent for Android.

Open the app-level build.gradle file for your project and add the following code to the dependencies section:


    dependencies {

        testImplementation 'junit:junit:4.13.2'
    
        testImplementation 'org.amshove.kluent:kluent-android:1.72')
    }

Once you're done, hit the Sync Now button so Gradle can download and install the Kluent Android library. In addition to the Kluent dependency, you need JUnit to run unit tests. That's why we had to add the dependency in the above code.

Step 2: Create a Class to Test

Before you start writing any tests, you need to have some code to test. So, create a new class under the main package of your project (the package where you have MainActivity.kt by default).

Right-click on the package main package name, select New > Kotlin Class/File, then save the new class as GeneralHelper.

Next, add the following method to the new class:


    fun convertNairaToDollar(nairaValue: Double) : Double {
        val exchangeRate: Double = 480.0
        return nairaValue/exchangeRate
    }

The above code is a simple method that converts an input value that represents the Nigerian Naira to its equivalent US Dollar value using a fixed exchange rate. In later steps, you'll write unit tests to verify that this method meets its functional requirement.

Step 3: Write a Unit Test

At this point, we have everything we need to write a test that uses Kluent to do assertions. In Android development, by default, unit tests live in the src/test directory. For the sake of this tutorial, you can create a new unit test class in that directory or use the default ExampleUnitTest.kt file. I'll be using the default file for this step.

First, let's write a test that verifies the convertNairaToDollar() method using JUnit's inbuilt assertion. Doing this will help us see how Kluent makes assertions more readable when compared to JUnit's assertion methods.

Open your unit test class file, and add the following method:


    private val utilsClass = GeneralHelper()
    @Test
    fun convertNairaToUSD() {
        assertEquals(1.0, utilsClass.convertNairaToDollar(480.0), 0.1)
    }

The above code creates an instance of the GeneralHelper class we created in step 2 in a utilsClass variable. Then there's a convertNairaToUSD function that's annotated with @Test so JUnit can run the function as a test. Nested inside the convertNairaToUSD() function is a single line of code that uses the JUnit assertEquals() method to verify that 480 Naira equals 1 USD. By merely looking at this assertion, it's not clear what we're testing for.

Here's where Kluent comes in. Let's write the same test again, using Kluent for assertion.

Add the following code to your test class just after the previous convertNairaToUSD() method:


    @Test
    fun convertNairaToUSDKluent() {
        utilsClass.convertNairaToDollar(480.0) shouldBeEqualTo 1.0
    }

The above code is another test method that tests for the same thing as the last test. The only difference is that it uses Kluent's shouldBeEqual function. To the left is the result from the function we're testing, and the expected value is on the right. This test is more human-readable, as it reads like "result should be equal to expectation."

Step 4: Run the Tests

To run each test, click on the green play button next to the associated test method. Then wait for Gradle to start the test Task.

Capabilities of Kluent

Kluent has many capabilities that make writing assertions easy and fun. The following are some of the most useful features.

  • Support for backticks: You can use backticks to write even more readable assertions with Kluent.
  • Chain assertions: Kluent makes it possible to chain assertions. That's to say, you can check whether a result meets multiple expectations by just nesting each assertion.
  • Assertions for lists: Kluent has assertion methods for verifying common issues related to lists and collections. Some of these assertions include shouldBeIn, shouldContainAll, and shouldNotContainAny.
  • Define custom assertions: You can extend the functionalities of Kluent by writing custom assertions that meet your specific needs.
Kluent and the other testing frameworks you might have make testing faster and more accurate

Conclusion

In this post, you learned what Kluent is. Kluent is a "Fluent Assertion" library for Kotlin. It helps make tests more human-readable. We also discussed some advantages and disadvantages of Kluent.

In order to explain how Kluent works better, we walked through a tutorial on how to use Kluent in an Android Studio Kotlin project.

Kluent and the other testing frameworks you might have, make testing faster and more accurate. However, most testing frameworks require technical initial configuration/setup, and you need coding knowledge to write tests. If you'd like to do automated testing with less code and configuration, you can use a tool like Waldo to test your Kotlin app. Waldo offers a no-code testing tool for testing mobile applications.

Finally, to continue learning about Kluent, we recommend you check out the official documentation here. Until next time!

Automated E2E tests for your mobile app

Waldo provides the best-in-class runtime for all your mobile testing needs.
Get true E2E testing in minutes, not months.

Reproduce, capture, and share bugs fast!

Waldo Sessions helps mobile teams reproduce bugs, while compiling detailed bug reports in real time.