How to define a JUnit method rule in a test suite?

前端 未结 5 1864
孤街浪徒
孤街浪徒 2020-11-30 08:03

I have a class that is a JUnit suite of JUnit test classes. I would like to define a rule on the suite to do something to the database before and a

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 08:31

    Have you tried to use "test class hierarchy" ? I often use abstract test class to share test or fixture. For example, all my DB test are initialising an embedded datasource. I first create an abstract "DbTestCase" class which handles the init logic. Then all subclass will benefit the test and the fixtures.

    However, sometimes I encounter problem when my test cases requires many test/fixture-logic that I can't store in a single hierarchy. In this case, only aspect programming solves the issue. Marking test/fixture-logic through specific annotation/interfaces that any required-class can implement.

    You can optionnaly consider handling the "aspect" using a custom runner that will "inject" test/fixture-logic depending on annotation/interface of tested classes.

提交回复
热议问题