Class JunitServerRunner

java.lang.Object
org.junit.runner.Runner
org.junit.runners.ParentRunner<org.junit.runners.model.FrameworkMethod>
org.junit.runners.BlockJUnit4ClassRunner
com.github.mjeanroy.junit.servers.junit4.JunitServerRunner
All Implemented Interfaces:
org.junit.runner.Describable, org.junit.runner.manipulation.Filterable, org.junit.runner.manipulation.Orderable, org.junit.runner.manipulation.Sortable

public class JunitServerRunner extends org.junit.runners.BlockJUnit4ClassRunner
Runner that will start and stop embedded server before tests. This runner will also add some custom rules to inject data to test classes.

How to use?

Simply set the JunitServerRunner class with the JUnit RunWith annotation:

   @RunWith(JunitServerRunner.class)
   public class MyTest {

     // Get the server
     @TestHttpServer
     private static EmbeddedServer server;

     // Get a client to query embedded server
     @TestHttpServer
     private HttpClient client;

     @Test
     public void testGET() {
       HttpResponse rsp = client.prepareGet("/path")
         .acceptJson()
         .execute();

       Assert.assertTrue(rsp.status() == 200);
     }
   }
 

Should I use the ServerRule or the runner?

The runner should be used, but due to the limitation of JUnit (only one runner can be used), the rule can be used if you need to use a custom runner.
See Also:
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Create runner starting an embedded server.
    protected
    JunitServerRunner(Class<?> klass, EmbeddedServer<?> server)
    Create runner with given embedded server.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected List<org.junit.rules.TestRule>
     
    protected List<org.junit.rules.TestRule>
     

    Methods inherited from class org.junit.runners.BlockJUnit4ClassRunner

    collectInitializationErrors, computeTestMethods, createTest, createTest, describeChild, getChildren, isIgnored, methodBlock, methodInvoker, possiblyExpectingExceptions, rules, runChild, testName, validateConstructor, validateFields, validateInstanceMethods, validateNoNonStaticInnerClass, validateOnlyOneConstructor, validateTestMethods, validateZeroArgConstructor, withAfters, withBefores, withPotentialTimeout

    Methods inherited from class org.junit.runners.ParentRunner

    childrenInvoker, classBlock, createTestClass, filter, getDescription, getName, getRunnerAnnotations, getTestClass, order, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses, withInterruptIsolation

    Methods inherited from class org.junit.runner.Runner

    testCount

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JunitServerRunner

      public JunitServerRunner(Class<?> klass) throws org.junit.runners.model.InitializationError
      Create runner starting an embedded server.
      The embedded server implementation is automatically detected using classpath detection, so one of junit-servers-jetty or junit-servers-tomcat dependency must be added to the classpath. In case of a conflict (i.e if both dependency are available in the classpath), junit-servers-jetty will be used.
      Parameters:
      klass - Running class.
      Throws:
      org.junit.runners.model.InitializationError - If an error occurred while starting embedded server.
    • JunitServerRunner

      protected JunitServerRunner(Class<?> klass, EmbeddedServer<?> server) throws org.junit.runners.model.InitializationError
      Create runner with given embedded server.
      Parameters:
      klass - Running class.
      server - The embedded server to use.
      Throws:
      org.junit.runners.model.InitializationError - If an error occurred while starting embedded server.
  • Method Details

    • classRules

      protected List<org.junit.rules.TestRule> classRules()
      Overrides:
      classRules in class org.junit.runners.ParentRunner<org.junit.runners.model.FrameworkMethod>
    • getTestRules

      protected List<org.junit.rules.TestRule> getTestRules(Object target)
      Overrides:
      getTestRules in class org.junit.runners.BlockJUnit4ClassRunner