0

I have an abstract testcase "AbstractATest" for an interface "A". It has several test methods (@Test) and one abstract method:

protected abstract A unit();

which provides the unit under testing. No i have multiple implementations of "A", e.g. "DefaultA", "ConcurrentA", etc.

My problem: The testcase is huge (~1500 loc) and it's growing. So i wanted to split it into multiple testcases. How can organize/structure this in Junit 4 without the need to have a concrete testcase for every implementation and abstract testcase.

I want e.g. "AInitializeTest", "AExectueTest" and "AStopTest". Each being abstract and containing multiple tests. But for my concrete "ConcurrentA", i only want to have one concrete testcase "ConcurrentATest".

I hope my "problem" is clear.

EDIT
Looks like my description was not that clear.
Is it possible to pass a reference to a test?
I know parameterized tests, but these require static methods, which is not applicable to my setup. Subclasses of an abstract testcase decide about the parameter.

4

3 回答 3

0

我不知道您是否已经解决了这个问题,但我建议您保留一个抽象测试,但将该抽象类中方法的实现外部化。

例如,使用 Initialize 测试创建一个类

public class InitializeTester {
    protected static void testInitializeA(A unit) {
        ...
                assertSomething ...
    }

    protected static void testInitializeB(A unit) {
        ...
                assertSomething ...
    }

}

执行测试的类:

public class ExecuteTester {
    protected static void testExecuteA(A unit) {
        ...
                assertSomething ...
    }

    protected static void testExecuteB(A unit) {
        ...
                assertSomething ...
    }

}

然后你的实际抽象测试器:

public abstract class ATester {
    @Test
    public void testInitializA() {
        InitializeTester.testInitializeA(unit());
    }

    @Test
    public void testInitializB() {
        InitializeTester.testInitializeB(unit());
    }

    @Test
    public void testExecuteA() {
        testExecuteTester.testExecuteA(unit());
    }

    @Test
    public void testExecuteB() {
        testExecuteTester.testExecuteB(unit());
    }

    abstract protected A unit(); 
}

您最终可能会得到包含许多方法的抽象测试类,但它们都会非常短,因为它们会将控制权传递给您的 Tester 类。

于 2010-03-09T04:13:06.373 回答
0

为什么不让每个具体的测试用例通过一个名为 via 的方法初始化一个具体的测试实例@Before。即,您在每次测试之前初始化具体实例的新实例。然后,抽象测试类可以提供针对此实例的测试(通过抽象类中的受保护字段引用)。

因此,每个具体实例都会有一个测试类,这些只会提供一个新实例来进行测试。这些派生的抽象测试类提供所有测试。当您创建一个新的具体类型时,您只需要一个新的具体测试类来实例化该类的一个实例。

于 2010-02-17T22:18:58.627 回答
0

您可以向测试方法引入一个参数,并使用一个 @DataProvider 创建所有要测试的类的实例。好的,一次测试。

于 2010-02-17T22:30:30.857 回答