title: How can you wrap a JUnit 5 Test
body: <p>In JUnit 4 you could use a Rule to wrap a test so that you could execute code both before and after a test had run. In most cases this could be accomplished with an @Before and @After method or an ExternalResource rule. However some control flow constructs (like try-with-resources) cannot be split into two methods. In most cases, there are alternatives to these constructs which allow you to split them into two methods. For example, with try-with-resources, you can manually acquire and close a resource instead of using a try block.</p>
  
  <p>The specific problem that I have run into is that the database library I use, jOOQ, only has transaction methods that take a callback. (See <a href="https://www.jooq.org/doc/latest/manual/sql-execution/transaction-management/" rel="nofollow noreferrer">https://www.jooq.org/doc/latest/manual/sql-execution/transaction-management/</a>) You cannot call something like:</p>
  
  <pre><code>context.startTransaction()
  doStuff()
  context.commit() // Or rollback()
  </code></pre>
  
  <p>In JUnit4 this is ok because you can write a rule like so (in Kotlin, but the equivalent works in Java):</p>
  
  <pre><code>class TransactionRule(private val dbSessionManager: DBSessionManager) : TestRule {
      override fun apply(base: Statement, description: Description): Statement {
          return object : Statement() {
              override fun evaluate() {
                  dbSessionManager.transaction {
                      base.evaluate()
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Is there anything similar in JUnit 5?</p>