vendredi 3 mai 2019

How to give @After @Before annotation to an Interface

I'm setting up tests and for each of them I need to do actions. I don't want to implement it in the current abstract class and want to use a Interface. I see we can set a default method. But it doesn't work properly.

I try to create @Before and @After method in the abstract class and call default method from the interface that I implemented. And this is working. But when I directly but the @Before and @After in the interface, it doesn't work.

Someone can tell me what I'm doing wrong ?

Here is my test :

public class myTestClass extends AbstractTest {
    @Test
    public void myTest1 {
    System.out.println("Test List: " + myList.size());
        // ...
    }
}

My abstract class were I called some methods and implements the interface :

public abstract class AbstractTest implements InterfaceTest {
    // ...
}

And here is my interface were I want to call before and after method :

public interface InterfaceTest {
    List<String> myList = new ArrayList<>();

    default void before() {
        System.out.println("TEST INIT Before");
        myList.addAll(Arrays.asList("test1", "test2"));
    }

    @After
    default void after() {
        System.out.println("TEST INIT After");
        myList.clear();
    }
}

I expected to have :

TEST INIT Before
Test List: 2
TEST INIT After

But my actual result is :

Test List: 0

Thank you for reading

Aucun commentaire:

Enregistrer un commentaire