我正在处理项目中的一些时间,在执行单元测试时,我不确定是否已发现错误,或者是否误解了文档。 ValueRange.of(long min, long maxSmallest, long maxLargest) 的Javadocs状态:

 * @return the ValueRange for min, smallest max, largest max, not null
 * @throws IllegalArgumentException if
 *     the minimum is greater than the smallest maximum,
 *  or the smallest maximum is greater than the largest maximum

我的阅读方式是,如果传递给min的值大于maxSmallest的值,则应引发异常。但是,以下代码有效:
ValueRange vr = ValueRange.of(5, 2, 10);
我认为这应该失败。 min的值大于最小的最大值,但不会引发异常。此外,在打印结果ValueRange时,它看起来很奇怪:5-2/10

这是一个错误吗?还是我只是不了解javadocs?

最佳答案

它是一个错误JDK-8239520,在JDK 15 fixed中已经是Build 12

我已经基于ValueRange.of() API为不同的值范围创建了单元测试:

package org.mycompany.mavenproject1;

import java.time.temporal.ValueRange;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class ValueRangeTest {

    @ParameterizedTest
    @CsvSource({
        "5, 2" // the minimum is greater than the maximum
    })
    public void testFixedValueRange(long min, long max) {
        Executable when = () -> ValueRange.of(min, max);

        assertThrows(IllegalArgumentException.class, when,
                () -> "min: " + min + ", max: " + max);
    }

    @ParameterizedTest
    @CsvSource({
        "5, 2, 10", // the minimum is greater than the smallest maximum - THIS FAILS!
        "2, 10, 2" // the smallest maximum is greater than the largest maximum
    })
    public void testVariableValueRange(long min, long maxSmallest, long maxLargest) {
        Executable when = () -> ValueRange.of(min, maxSmallest, maxLargest);

        assertThrows(IllegalArgumentException.class, when,
                () -> "min: " + min + ", maxSmallest: " + maxSmallest + ", maxLargest: " + maxLargest);
    }

    @ParameterizedTest
    @CsvSource({
        "5, 1, 2, 10", // the smallest minimum is greater than the smallest maximum
        "1, 2, 10, 2", // the smallest maximum is greater than the largest maximum
        "1, 11, 2, 10" // the largest minimum is greater than the largest maximum
    })
    public void testFullyVariableValueRange(long minSmallest, long minLargest, long maxSmallest, long maxLargest) {
        Executable when = () -> ValueRange.of(minSmallest, minLargest, maxSmallest, maxLargest);

        assertThrows(IllegalArgumentException.class, when,
                () -> "minSmallest: " + minSmallest + ", minLargest: " + minLargest + ", maxSmallest: " + maxSmallest + ", maxLargest: " + maxLargest);
    }

}

所有方案都通过,但只有一种-if the minimum is greater than the smallest maximum
Tests run: 6, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.087 s <<< FAILURE! - in org.mycompany.mavenproject1.ValueRangeTest
testVariableValueRange{long, long, long}[1]  Time elapsed: 0.034 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: min: 5, maxSmallest: 2, maxLargest: 10 ==> Expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown.
    at org.mycompany.mavenproject1.ValueRangeTest.testVariableValueRange(ValueRangeTest.java:30)

这使我相信这是ValueRange中的错误。

08-28 21:41