web-dev-qa-db-ja.com

JUnit 5(または他のテストJavaライブラリ)の配列を使用したよりスマートな方法でのパラメーター化

私はこのテストをパラメータ化しようとしています:

_@Test
public void reverseQuote(double[] qsp) throws Exception {
...}
_

たとえば、qspのように配列ValueSourceを初期化する簡単な方法が存在しないのは私にはばかげているようです。

_@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
    assertNotNull(argument);
}
_

私の目的は、@ValueSource(doublesArray = {new double[]{1.0, 2.0, 3.0}})のようなことをすることです(これでエラーが返されます)。似たようなものを許すものはありませんか? その他の回答 _@MethodSource_や_@ConvertWith_の使用など、手の込んだ方法のみを提案しているようです。

他のテストライブラリを実装する回答も受け入れます。

20
Lore

Junitパラメーター化テストとYAML解析を組み合わせて使用​​することを検討する必要があるかもしれません。

@RunWith(Parameterized.class)
public class AnotherParameterizedTest {

    private final HashMap row;

    @Parameterized.Parameters(name="Reverse Lists Tests # {index}:")
    public static List<Map<String, Object>> data() {
        final TestData testData = new TestData(""+
             "|   ID   |       List         |  Expected   |                \n"+
             "|   0    |    [1, 2, 3]       |  [3, 2, 1]  |                \n"+
             "|   1    |    [2, 3, 5]       |  [3, 2, 1]  |                \n"+
             "|   2    |    [5, 6, 7]       |  [ 7, 6, 5] |                \n"
        );
        // parsing each row using simple YAML parser and create map per row
        return testData.getDataTable();
    }

    // Each row from the stringified table above will be 
    // split into key=value pairs where the value are parsed using a 
    // yaml parser. this way, values can be pretty much any yaml type
    // like a list of integers in this case. 
    public AnotherParameterizedTest(HashMap obj) {
        this.row = obj;
    }

    @Test
    public void test() throws Exception {
        List orgListReversed = new ArrayList((List) row.get("List"));
        Collections.reverse(orgListReversed);
        assertEquals((List) row.get("Expected"), orgListReversed);
    }

}

文字列を使用する代わりに、Excelリーダーを使用して単純なExcelテーブルで同じことを行います。値にYAMLを使用して、各行を1つのマップに解析します。

Junit IDEテスト結果

Junit Jupiterを使用してテストしたものと同じものが、IDE Runnerでより良い結果をもたらします。

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

import de.deicon.yatf.runner.dsl.TestData;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import Java.util.Collections;
import Java.util.List;
import Java.util.Map;

public class FirstTest {

    @ParameterizedTest
    @MethodSource("testTable")
    public void test(Map row){
        List reversedList = (List) row.get("List");
        Collections.reverse(reversedList);
        assertEquals((List)row.get("Expected"), reversedList);
    }

    static List<Map<String, Object>> testTable() {
        return new TestData(""+
                "|ID|   List                  |Expected               |         \n"+
                "|0 | [1,2,3]                 | [3,2,1]               |         \n"+
                "|1 | [hans, peter, klaus]    | [klaus, peter, hans]  |         \n"
        ).getDataTable();
    }

}

enter image description here

8
Dieter

わかりました、これは奇妙な答えになるでしょうが、それはうまくいき、それをするのはちょっと楽しかったです。

まず第一に、あなたのやり方は不可能です。 JUnitまたは関連するAPIのためではなく、Java -- 有効なアノテーションタイプ要素 (アノテーション引数はプリミティブ、文字列、クラス、列挙型、その他のアノテーションのみ)およびそれらすべての配列)。

2番目のこと:最初のものを回避することができます。これをチェックして:

@ArraySources(
  arrays = {
    @ArraySource(array = {1, 2, 3}),
    @ArraySource(array = {4, 5, 6}),
    @ArraySource(array = {7, 8, 9})
  }
)

それが言うように、アノテーションは引数として他のアノテーションとそれらの配列を持つことができるので、ここではそれらの2つのルールを使用しています。

3番目のこと:それはどのように役立ちますか?独自のアノテーション+引数プロバイダーを追加できます。JUnit5はそのように拡張可能です。

両方の注釈:

@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(ArrayArgumentsProvider.class)
public @interface ArraySources {
    ArraySource[] arrays();
}

@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ArraySource {
    int[] array() default {};
}

注釈に基づく引数プロバイダー:

public class ArrayArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<ArraySources> {
    private List<int[]> arguments;

    public void accept(ArraySources source) {
        List<ArraySource> arrays = Arrays.asList(source.arrays());

        this.arguments = arrays.stream().map(ArraySource::array).collect(Collectors.toList());
    }

    public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
        return this.arguments.stream().map(Arguments::of);
    }
}

そしてそれらを使用した最終テスト:

public class ArraySourcesTest {
    @ParameterizedTest
    @ArraySources(
            arrays = {
                    @ArraySource(array = {1, 2, 3}),
                    @ArraySource(array = {4, 5, 6}),
                    @ArraySource(array = {7, 8, 9})
            }
    )
    void example(int[] array) {
        System.out.println(Arrays.toString(array));
        System.out.println("Test Over");
    }
}

/* Output
[1, 2, 3]
Test Over
[4, 5, 6]
Test Over
[7, 8, 9]
Test Over
*/

あなたは@MethodSource複雑で、まあ、私はこの問題に失敗したと思いますが、それは機能します。それは明らかに単純化および強化することができます(注釈引数をデフォルトとして命名するように-値-そして私はアイデアを示すためにintに対してのみそれを行いました)。既存の機能(ArgumentsProviderおよびArgumentSources)で同じことを達成できるかどうかはわかりませんが、これはより具体的に見え(配列を使用していることがわかります)、JUnit5を拡張する可能性を示しています。他の場合に役立ちます。

7
Shadov

テストに Spock を使用するのが好きですJavaコード。これは、JUnit4の上にあるGroovyベースのテストフレームワークです。Spockのパラメーター化されたテストは組み込み機能です。 :

def "The reverseQuote method doesn't return null"(double[] qsp) {

    when: "reverseQuote is called"
    double[] rev = reverseQuote(qsp)

    then: "the result is not null"
    null != rev

    where: "there are various input values"
    qsp << [
        [0.1, 0.2, 0.3] as double[],
        [1.0, 2.0, 3.0] as double[]
    ]
}

...または、テストデータを表形式でレイアウトすることもできます。

def "The reverseQuote method reverses the input array"(List qsp, List expected) {

    when: "reverseQuote is called"
    double[] rev = reverseQuote(qsp as double[])

    then: "the result is the reverse of the input"
    expected as double[] == rev

    where: "there are various input values"
    qsp             | expected
    [0.1, 0.2, 0.3] | [0.3, 0.2, 0.1]
    [1.0, 2.0, 3.0] | [3.0, 2.0, 1.0]
}

as double[]普及は、Groovyが配列をリストに自動的に変換するという不幸な結果であるため、実際に配列を必要とするJavaコードを操作する特定の場合には、明示的にそれらをキャストバックする必要があります。

5
user31601

アレイ:

    static Stream<Arguments> yourTest () {
    return Stream.of(
            Arguments.of((new int[] { 2, 1, 2, 3, 4 }), 3),
            Arguments.of((new int[] { 2, 2, 0 }), 3),
            Arguments.of((new int[]{1, 3, 5} ) ,0 )     
            );
}
// if not Array : List : Arguments.of((Arrays.asList(0, 1) ), 0.5)...


@ParameterizedTest(name = "{index} => array   = {0} ),  expected = {1} ") 
@MethodSource("yourTest")
void shoultCountEvens(  int[] array, int expected) {
    assertEquals( CountEvens.countEvens(array),  expected ); 
}


public class CountEvens {
public static int countEvens(int[] nums) {
    long count = Arrays.stream(nums)
    .filter(a -> a %  2 == 0)
    .count();
return Math.toIntExact(count);
}}
0
Fox user9219598

JUnitのドキュメントでは、@MethodSourceの使用が提案されています

@ParameterizedTest
@MethodSource("range")
void testWithRangeMethodSource(double argument) {
    assertNotEquals(9.0, argument);
}

static DoubleStream range() {
   return DoubleStream.range(0.0, 20.0);
}

それ以外の場合は、これを使用することを検討できます: https://junit.org/junit5/docs/5.0.2/api/org/junit/jupiter/params/provider/ValueSource.html#doubles

@ParameterizedTest
@ValueSource(doubles = { 1, 2, 3 })
public void test(double numberToTest){
   //do whatever
}
0
Ben Sch