如何 运行 JUnit 4 数据驱动测试多数据

How to run JUnit 4 Data driven test with multiple data

我正在尝试为具有两个重载方法的 class 编写 junit 测试。这是我正在测试的代码

public class Solution {
    public static String getDurationString(int minute, int seconds) {
        if (minute < 0) {
            return "Invalid Value";
        } else if (seconds < 0 || seconds > 59) {
            return "Invalid Value";
        } else {
            int hours = minute / 60;
            minute %= 60;
            return getTimeString(hours) + "h " + getTimeString(minute) + "m " + seconds + "s";
        }
    }

    public static String getDurationString(int seconds) {
        if (seconds < 0) {
            return "Invalid Value";
        }

        int minutes = seconds / 60;
        seconds %= 60;
        int hours = minutes / 60;
        minutes %= 60;

        return getTimeString(hours) + "h " + getTimeString(minutes) + "m " + seconds + "s";
    }

    //returns value in xx format
    private static String getTimeString(int val) {
        if (val < 10) {
            return "0" + val;
        }
        return "" + val;
    }
}

我能够使用 JUnit 的参数化运行器使用一些数据来测试 getDurationString(min,sec) 函数。我想使用相同的测试代码为 getDurationString(sec) 函数编写测试,但我不知道该怎么做。我看了一些 Whosebug question 但没有帮助(可能是因为我使用的是 JUnit4)。

知道如何使用 JUnit4 实现这一点吗?

UPDATE: Added test code for reference

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
class SolutionTest {

    @Parameterized.Parameters(name = "Test{index} -> {0} min {1} sec returns {2}")
    public static Iterable<Object[]> dataForTwoParameterMethod() {
        return Arrays.asList(new Object[][]{
                {-1, 10, "Invalid Value"}, //minutes < 0 returns "Invalid value"
                {1, -1, "Invalid Value"}, //Negative seconds value
                {61, 50, "01h 01m 50s"}, //valid value returns time in format "XXh YYm ZZs"
        });
    }

    int minute;
    int seconds;
    String durationString;

    public SolutionTest(int minute, int seconds, String durationString) {
        this.minute = minute;
        this.seconds = seconds;
        this.durationString = durationString;
    }

    @Test
    public void getDurationString() {
        assertEquals(durationString, Solution.getDurationString(minute, seconds));
    }

    @Test @Ignore("TODO: Not able to find a way to inject two different data into one JUnit Test")
    public void testGetDurationString() {
        //TODO: how to use two data in single data driven test
    }
}

如果我没理解错的话,你有一组参数:

{-1, 10, "Invalid Value"}, //minutes < 0 returns "Invalid value"
{1, -1, "Invalid Value"}, //Negative seconds value
{61, 50, "01h 01m 50s"}, //valid value returns time in format "XXh YYm ZZs"

你用来测试双参数方法getDurationString。而且-这是我的理解-您还想提供第二组参数,也许

{-1, "Invalid Value"}, //seconds < 0 returns "Invalid value"minutes, "
{0, "00h 00m 00s"}, //boundary case, everything zero
{1, "00h 00m 01s"}, //last digit (seconds)
{10, "00h 00m 10s"}, //but last digit (seconds)
{59, "00h 00m 59s"}, //boundary case
{60, "00h 01m 00s"}, //boundary case, last digit (minutes)
...

测试单参数方法getDurationString。但是,要使用 JUnit4 执行此操作,您将必须创建第二个测试 class - 您不能将两组不同的参数放入同一 class.

您可以在一个 class 参数化测试中包含许多测试方法 - 但它们都将在同一组参数上运行,如 @Parameterized.Parameters 中所定义。

请注意,这与 JUnit5 中实现参数化测试的方式不同:在 JUnit5 中,对于每个单独的参数化测试方法,可以单独决定测试方法应接收哪些输入参数。这是通过使用测试数据来自的信息注释每个测试方法来完成的:https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests