title: How do I manage unit test resources in Kotlin, such as starting/stopping a database connection or an embedded elasticsearch server?
body: <p>In my Kotlin JUnit tests, I want to start/stop embedded servers and use them within my tests.</p>
  <p>I tried using the JUnit <code>@Before</code> annotation on a method in my test class and it works fine, but it isn't the right behaviour since it runs every test case instead of just once.</p>
  <p>Therefore I want to use the <code>@BeforeClass</code> annotation on a method, but adding it to a method results in an error saying it must be on a static method.  Kotlin doesn't appear to have static methods.  And then the same applies for static variables, because I need to keep a reference to the embedded server around for use in the test cases.</p>
  <p>So how do I create this embedded database just once for all of my test cases?</p>
  <pre><code>class MyTest {
      @Before fun setup() {
         // works in that it opens the database connection, but is wrong 
         // since this is per test case instead of being shared for all
      }
  
      @BeforeClass fun setupClass() {
         // what I want to do instead, but results in error because 
         // this isn't a static method, and static keyword doesn't exist
      }
  
      var referenceToServer: ServerType // wrong because is not static either
  
      ...
  }
  </code></pre>
  <p><strong>Note:</strong> <em>this question is intentionally written and answered by the author (<a href="https://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/">Self-Answered Questions</a>), so that the answers to commonly asked Kotlin topics are present in SO.</em></p>