Template for setting @BeforeClass and @Before Methods

I have a situation where I have something like the following:

public class SomeTest extends AbstractMyTest {

    @Test
    public void something() {
        //Test something, related to the AbstractMyTest config
    }

    @Override
    public String getConfiguration() {
        return "myConfigFile.ini";
    }
}

public abstract class AbstractMyTest {
    @Before
    public void before() {
        //Do some init stuff that calls getConfiguration()...
    }

    abstract String getConfiguration();
}

Now I'm considering getting rid of the AbstractMyTest class and having something like the following:

@MyTestConfig(value="myConfigFile.ini")
public class SomeTest {
    @Test
    public void something() {
        //Test something, related to the AbstractMyTest config
    }
}

So, I can have a custom Runner that does what the AbstractMyTest class responds to. I would like to be able to do some things in @BeforeClass or @Before, without having to do this in every TestClass. How will such a runner be structured?

+4
source share
1 answer

I don’t quite understand why you need custom Runnerhere.

, Rule s, , . , ExternalResource :

public class SomeTest {
    @Rule
    public MyTestRule myRule = new MyTestRule("myConfigFile.ini");
    ...
}

public class MyTestRule extends ExternalResource {
    ...
    protected void before() { ... }
    ...
}
+2

Source: https://habr.com/ru/post/1542324/


All Articles