I have multiple parameterized test classes taking exactly the same parameters. When I run the tests each one calls its data() method that returns the parameters for the test execution.

My problem is that all my data() methods are the same and create the same parameters and I can't figure out how to remove this duplicated code.

The goal is to have one single function that compute the parameters for all the test classes.

If you have any idea how to do that it would help me a lot.

Thanks

有帮助吗?

解决方案

Offhand, I'd say you have the two classic OO options - inheritance and inclusion.

With inheritance, you just put the data() method in some base class:

public class MyTestCaseBase {
    @Parameters
    public static Collection<Object[]> data() {
        return ...
    }
}

public class MyTest extends MyTestCaseBase {
    // tests
}

With inclusion you leave yourself the option to extend different classes, but need to do some more "plumbing" yourself:

public class MyTestParameters {
    public static Collection<Object[]> data() {
        return ...
    }
}

public class MyTest {
    @Parameters
    public static Collection<Object[]> data() {
        return MyTestParameters.data();
    }

    // tests...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top