jeudi 30 mai 2019

How to test multiple objects with same method in 1 test class?

I have 2 different objects: C c and B b. B and C implement interface A so that they can use method called color() that is present in interface A. I already made unit test for class B that test the color() method that B has implemented. So what I want to do now is test the color() method in class C with the same unit test of class B. Thus I want to test both of them in the same test class that I have made for class B.

To achieve this, one of my friends said that I would have to make use of the parallel class hierarchy of those classes. But I don't really know how I should implement that in my test.

This is what I have in terms of code:

private static Sprites sprites;
private static B b;

@BeforeAll
static void setUp() {
    sprites = mock(Sprites.class);
    b = new B(sprites);
}

@Test
void testing_color_method() {

    M m = mock(M.class);

    b.color(m);

    verify(sprites).getSpritesOf(m);

}

//... some more tests

I am using JUnit 5 and I know I could use @ParameterizedTest to inject objects B and C in the test to let them use the same unit tests, I have also done some google search about this but none of the search results had it about this kinds of cases where 2 objects need to be injected in the 1 test. So how should I refactor the code so that I can inject class B and C to let them use the same unit tests that I already crated for B?

Aucun commentaire:

Enregistrer un commentaire