Java jUnit: Test suite code to run before any test classes

后端 未结 5 522
暖寄归人
暖寄归人 2020-12-31 01:33

I have a test suite class:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    GameMasterTest.class,
    PlayerTest.class,
})
public class BananaTestSuite { 
         


        
5条回答
  •  抹茶落季
    2020-12-31 02:09

    Make a super test class containing the method annotated with @BeforeClass. Extend all the test classes from this class, eg,

    @Ignore
    public class BaseTest {
        @BeforeClass
        public static void setUpBaseClass() {
            //Do the necessary setup here
        }
    }
    

    This method will run before any @BeforeClass annotated method in the subclasses: Source. Ensure that this method name is not used in any subclass to prevent shadowing.

    If you need to run this just once (eg, if you are creating/initializing a large resource for all tests to use), set a flag in the super class to check whether the method ran or not.

    This design will also ensure that if you change the test runner, you need not change anything else.

提交回复
热议问题