1

I want to implement groovy parametrized test, where test names will be built using parameter values. I know how to do it in java (also I've found the java answer here, but not groovy)

So I need to improve my groovy parametrized unit test with same option:

import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized)
class TestSimple {

    String numberA
    String numberB
    String expectedResult

    TestSimple(numA, numB, expected) {
        numberA = numA
        numberB = numB
        expectedResult = expected
    }

    @Parameterized.Parameters
    static data1() {
        [
                [null, "asd|asd|||ASD||ASD", 'result for first case'],
                ['09909', "asd|asd|||ASD||ASD", 'result for second case']

        ]*.toArray()
    }

    @Test
    void testLogonRequest1() throws Exception {
        println numberA + ":" + numberB
    }
}

to replace ordered number with parameter value

TestSimple
  [0]
    testLogonRequest1[0]
  [1]
    testLogonRequest1[1]

Expected result similar to:

TestSimple
  [used parameters: <numberA0value>, <numberB0value>]
    testLogonRequest1[expected result: <expectedResult0Value>]
  [used parameters: <numberA1value>, <numberB1value>]
    testLogonRequest1[expected result: <expectedResult1Value>]

Any ideas?

Sergii
  • 7,044
  • 14
  • 58
  • 116
  • I don't think it is possible just using `org.junit.runners.Parameterized`, the message in `[]` has to be same, maybe you can write your SuiteClasses to achieve this effect. – aristotll Aug 18 '17 at 06:05
  • do you have any exception? – daggett Aug 18 '17 at 07:31
  • btw. why you did not specified the `data1()` function return type? try to set `@groovy.transform.CompileStatic` annotation for your class and fix all the compile errors. – daggett Aug 18 '17 at 07:37
  • @daggett no exceptions here. I've not specified types and so on, and done as it is, because of i'm new in groovy :). If I know how to do better, i do it. Is return type important for my issue? – Sergii Aug 18 '17 at 07:57
  • @aristotll. It seems you know how to do it. Could you pay my attention which annotations i should use for my purpose (I would research them)? – Sergii Aug 18 '17 at 08:01
  • You can see the source code of `org.junit.runners.Parameterized#createTestWithParameters` and `org.junit.runners.Parameterized#getParametersMethod`, and understand why I say the message in [] has to be same. It is not an annotation choice problem. – aristotll Aug 18 '17 at 08:23
  • So you have to write your own version of `org.junit.runners.Parameterized`. – aristotll Aug 18 '17 at 08:24
  • Is Spock Framework an option? – rafaelim Aug 18 '17 at 20:18
  • I do not know `spock` yet, but if it's useful - of course. – Sergii Aug 19 '17 at 09:35

0 Answers0