Archive for category kotlin

About JUnit 5 parameterized tests

Interesting article about JUnit 5 parameterized tests using BDD (Behaviour-Driven Development) style. It shows an example of using @CsvSource as arguments source and a specific implementation of an argument converter. Have you ever used parameterized tests in your unit tests?

Tags: , , , ,

About the Kotlin programming language

Kotlin is a statically typed language which is fully interoperable with Java.

Recently my friend Andre showed me Kotlin‘s nice syntax and I considered giving it a try.

In the meantime, my friend Leonnardo sent me this nice link which helps migrating from Java to Kotlin easily.

Let’s compare some syntax from Java and Kotlin and see the differences. Suppose we have some employees and we want to group them by their departments.

In Java we create an Employee class, build some employees and use the Streams API to group them by their departments:

public class Employee {

    private final String name;

    private final String department;

    public Employee(final String name, final String department) {
        this.name = name;
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public String getDepartment() {
        return department;
    }

    @Override
    public String toString() {
        return "Employee(name=" + name + ", department=" + department + ")";
    }
}

final Employee mark = new Employee("Mark", "Accounting");
final Employee john = new Employee("John", "Management");
final Employee smith = new Employee("Smith", "Administrative");
final Employee paul = new Employee("Paul", "Accounting");

final List employees = Arrays.asList(mark, john, smith, paul);

final Map> employeesByDepartment = employees
                .stream()
                .collect(Collectors.groupingBy(Employee::getDepartment));

In Kotlin we create a data class Employee, build some employees and use the collection built-in groupBy method to group them by their departments:

data class Employee(val name: String, val department: String)

val mark = Employee("Mark", "Accounting")
val john = Employee("John", "Management")
val smith = Employee("Smith", "Administrative")
val paul = Employee("Paul", "Accounting")

val employees = listOf(mark, john, smith, paul)

val employeesByDepartment = employees.groupBy { it.department }

As you can see, Kotlin has some syntactic sugar that makes it less verbose than Java.

If you haven’t considered trying Kotlin yet, I think it is worth giving it a try.

Tags: , , , , , ,