How to share JUnit BeforeClass logic among multiple test classes

后端 未结 3 1254
感情败类
感情败类 2021-01-31 17:13

Currently, all of my JUnit tests extend from a common base class that provides methods tagged with @BeforeClass and @AfterClass annotations - all thes

3条回答
  •  日久生厌
    2021-01-31 17:45

    I came across similar problem (Spring was not an option and i don't write TestSuites in maven projects) so i wrote simple junit runner to solve this problem.

    You need to write SharedResource class and mark your test to require that resource.

    public class SampleSharedResource implements SharedResource {
    public void initialize() throws Exception {
        //init your resource
    }
    

    }

    @RunWith(JUnitSharedResourceRunner.class)
    @JUnitSharedResourceRunner.WithSharedResources({SampleSharedResource.class})
    public class SharedResourceRunnerATest {
    ...
    

    Sources at https://github.com/eanlr/junit-shared-resources-runner

提交回复
热议问题