How does Junit @Rule work?

前端 未结 4 1446
予麋鹿
予麋鹿 2020-11-28 18:40

I want to write test cases for a bulk of code, I would like to know details of JUnit @Rule annotation feature, so that I can use it for writing test cases. Ple

4条回答
  •  离开以前
    2020-11-28 19:06

    The explanation for how it works:

    JUnit wraps your test method in a Statement object so statement and Execute() runs your test. Then instead of calling statement.Execute() directly to run your test, JUnit passes the Statement to a TestRule with the @Rule annotation. The TestRule's "apply" function returns a new Statement given the Statement with your test. The new Statement's Execute() method can call the test Statement's execute method (or not, or call it multiple times), and do whatever it wants before and after.

    Now, JUnit has a new Statement that does more than just run the test, and it can again pass that to any more rules before finally calling Execute.

提交回复
热议问题