jeudi 19 novembre 2020

Spock Extension - Extracting variable names from Data Tables

In order to extract data table values to use in a reporting extension for Spock, I am using the following code:

@Override
public void beforeIteration(IterationInfo iteration) {
    Object[] values = iteration.getDataValues();
}

This returns to me the reference to the objects in the data table. However, I would like to get the name of the variable that references the value.

For example, in the following test:

private static int test1 = 1;
private static int test2 = 2;
private static int test3 = 3;
private static int test4 = 4;
private static int test7 = 7;

def "Sum number #a with #b should result in #expected"(int a, int b, int expected) {
        given: "I have a number"

        when: "I sum with other number"
        int sum = a + b

        then: "the sum should be correct"
        sum == expected

        where:
        a       | b        | expected
        test1   | test1    | test2
        test3   | test4    | test7
}

Is there a way to receive the string "test1", "test1", "test2", etc instead of their values?

The reason for this is that I have complex objects and their toString() methods are not good for the reports.

Aucun commentaire:

Enregistrer un commentaire