Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>TestExecutionListeners</code> are a way to externalize reusable code that <em>instruments</em> your tests.</p> <p>As such, if you implement a <code>TestExecutionListener</code> you can reuse it across test class hierarchies and potentially across projects, depending on your needs.</p> <p>On the flip side, a <code>@BeforeClass</code> method can naturally only be used within a single test class hierarchy.</p> <p>Note, however, that JUnit also supports <a href="https://github.com/junit-team/junit/wiki/Rules" rel="noreferrer">Rules</a>: if you implement <code>org.junit.rules.TestRule</code> you can declare it as a <code>@ClassRule</code> to achieve the same thing... with the added benefit that a JUnit Rule can be reused just like a Spring <code>TestExecutionListener</code>.</p> <p>So it really depends on your use case. If you only need to use the "before class" functionality in a single test class or a single test class hierarchy, then you'd be better off going the simple route of just implementing a <code>@BeforeClass</code> method. However, if you foresee that you will need the "before class" functionality in different test class hierarchies or across projects, you should consider implementing a custom <code>TestExecutionListener</code> or JUnit Rule.</p> <p>The benefit of a Spring <code>TestExecutionListener</code> over a JUnit Rule is that a <code>TestExecutionListener</code> has access to the <code>TestContext</code> and therefore access to the Spring <code>ApplicationContext</code> which a JUnit Rule would not have access to. Furthermore, a <code>TestExecutionListener</code> can be <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#testcontext-tel-config-automatic-discovery" rel="noreferrer">automatically discovered</a> and <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#testcontext-tel-config-ordering" rel="noreferrer">ordered</a>.</p> <p><em>Related Resources</em>:</p> <ul> <li><a href="https://jira.spring.io/browse/SPR-8854" rel="noreferrer">SPR-8854</a></li> </ul> <p>Regards,</p> <p>Sam (author of the Spring TestContext Framework)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload