lundi 23 septembre 2019

How do I reuse JUnit test methods for different testing contexts?

Up to this point, I have been developing tests for the support of a certain application in my project. Now that I have a set of tests up and running, I need to provide support for another similar application. The result for both applications should be the same in my system, as they both produce the same actions but from different contexts. The thing is, I am looking for a way with which I could reuse the tests that I have made for the first one, without copying their code for the other test case.

I am currently using JUnit 4 under Java EE 8.

My situation goes something like this. Given a test like the one that follows:

class TestExample {
    Context context;

    @Before
    public void setUp() {
        context = new ContextA();
    }

    @After
    public void terminate() {
        context.dispose();
    }

    @Test
    public void test1() {
        context....
    }
}

abstract class Context {
    @Override
    public void dispose() {
        ...
    }
}

class ContextA extends Context {
}

class ContextB extends Context {
}

I would like to be able to do the test once for ContextA and then for ContextB.

Aucun commentaire:

Enregistrer un commentaire