Implement Table Driven Test in Java 14

Introduction

Java 14 has finally been released, so The source code introduced in the previous Implementing Table Driven Test in Java, I rewrote it using Records added as a Preview in Java 14.

Table Driven Test before Java 14

In the previous source code I kept the test cases in an inner class called TestCase. Constructors and getters were defined using Lombok.

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.junit.jupiter.api.*;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class MathTest {

    @Nested
    static class Pow {

        @AllArgsConstructor
        @Getter
        @Accessors(fluent = true)
        static class TestCase {
            private String name;
            private double x;
            private double y;
            private double expected;
        }

        @TestFactory
        Stream<DynamicNode> testPow() {
            return Stream.of(
                    new TestCase("pow(2,1)", 2, 1, 2),
                    new TestCase("pow(2,2)", 2, 2, 4),
                    new TestCase("pow(2,3)", 2, 3, 8),
                    new TestCase("pow(2,0)", 2, 0, 1),
                    new TestCase("pow(2,-1)", 2, -1, 0.5),
                    new TestCase("pow(2,+Inf)", 2, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY),
                    new TestCase("pow(2,-Inf)", 2, Double.NEGATIVE_INFINITY, 0),
                    new TestCase("pow(+Inf,2)", Double.POSITIVE_INFINITY, 2, Double.POSITIVE_INFINITY),
                    new TestCase("pow(-Inf,2)", Double.NEGATIVE_INFINITY, 2, Double.POSITIVE_INFINITY)
            ).map(testCase -> DynamicTest.dynamicTest(
                    testCase.name(),
                    () -> {
                        double result = Math.pow(testCase.x(), testCase.y());
                        assertEquals(testCase.expected(), result);
                    })
            );
        }
    }

}

Java 14 Table Driven Test

In this source code, Changed to keep test cases in record. By using record, I was able to reduce the amount of code compared to using Lombok.

import org.junit.jupiter.api.DynamicNode;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.TestFactory;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MathTest {

    @Nested
    static class Pow {

        record TestCase(String name, double x, double y, double expected) {
        }

        @TestFactory
        Stream<DynamicNode> testPow() {
            return Stream.of(
                    new TestCase("pow(2,1)", 2, 1, 2),
                    new TestCase("pow(2,2)", 2, 2, 4),
                    new TestCase("pow(2,3)", 2, 3, 8),
                    new TestCase("pow(2,0)", 2, 0, 1),
                    new TestCase("pow(2,-1)", 2, -1, 0.5),
                    new TestCase("pow(2,+Inf)", 2, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY),
                    new TestCase("pow(2,-Inf)", 2, Double.NEGATIVE_INFINITY, 0),
                    new TestCase("pow(+Inf,2)", Double.POSITIVE_INFINITY, 2, Double.POSITIVE_INFINITY),
                    new TestCase("pow(-Inf,2)", Double.NEGATIVE_INFINITY, 2, Double.POSITIVE_INFINITY)
            ).map(testCase -> DynamicTest.dynamicTest(
                    testCase.name(),
                    () -> {
                        double result = Math.pow(testCase.x(), testCase.y());
                        assertEquals(testCase.expected(), result);
                    })
            );
        }
    }

}

Summary

We hope that you will continue to improve the source code using new features.

Recommended Posts

Implement Table Driven Test in Java 14
Implement two-step verification in Java
Implement Basic authentication in Java
Implement math combinations in Java
2 Implement simple parsing in Java
Implement Email Sending in Java
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
3 Implement a simple interpreter in Java
Implement reCAPTCHA v3 in Java / Spring
Implement PHP implode function in Java
Try to implement Yubaba in Java
1 Implement simple lexical analysis in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Try to implement n-ary addition in Java
Java creates a table in a Word document
How to implement coding conventions in Java
Implement something like a stack in Java
Primality test Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
I made a primality test program in Java
Test Driven Development in Functional Language Elm (Chapter 15-16)
Test Driven Development in Functional Language Elm (Chapter 5-7)
I tried to implement deep learning in Java
I wrote a primality test program in Java
I tried to output multiplication table in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
I tried to implement Firebase push notification in Java
Comments in Java source
Azure functions in java
Implement test doubles in JUnit without using external libraries
Format XML in Java
Simple htmlspecialchars in Java
Java Unit Test Library-Artery-Sample
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Quickly implement a singleton with an enum in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java