jayunit100
jayunit100

Reputation: 17648

Junit Rules, TestCases, and inheritance : Do they work together?

Do Junit Rules apply to tests which are named in a superclass? It appears that they dont from some initial tests which I have run, wherein I inherit a Test class and Override named tests in my subclass. I have found:

1) @Ignore annotations do not cascade upward: They simply cause my subclass annotations to be ignored

2) USing @Rules which provide custom behaviour dont seem to work either: The @Rule annotations don't seem to effect the behaviour of a junit TestCase super class.

In any case, my goal is to ignore some tests in my superclass using a common Rule class - if this doesnt work, then I will have to manually override each and every test which fails, which results in a large amount of dead code added to our code base.

Upvotes: 3

Views: 2120

Answers (1)

John B
John B

Reputation: 32949

Having a @Rule in a sub-class will affect the run of the sub-class including all inherited tests. However, if the super-class is run independently of the sub-class the @Rule will not be present and therefore not affect the tests.

Upvotes: 2

Related Questions