Class StartupErrorRule

java.lang.Object
ru.vyarus.dropwizard.guice.test.StartupErrorRule
All Implemented Interfaces:
org.junit.rules.TestRule

public final class StartupErrorRule extends Object implements org.junit.rules.TestRule
Dropwizard exit with code 1 in case of exception during command run. In order to test error situations system exit must be prevented. Rule use system rules to intercept exit and output streams (system.out, system.err).

Extra dependency 'com.github.stefanbirkner:system-rules:1.16.0' is required.

In spock tests, 'then' section must contain rule.indicatorExceptionType (or thrown(CheckExitCalled)) as rule can't intercept this exception. 'then' section can be used for assertions after system exit and so no need to register custom callback in the rule.

For Junit 5 there is no direct alternative, but there are options:

  • For system exit capturing you can use https://github.com/tginsberg/junit5-system-exit
  • For system streams capturing there is no alternatives, but in any case it is impossible to do for parallel tests
Since:
16.03.2017
  • Method Details

    • create

      public static StartupErrorRule create()
      Use with spock tests or when no assertions required after system exit call.
      Returns:
      rule instance
    • create

      This is useful for junit tests, because there is no other way to check anything after exit call. In spock, 'then' section is always called and so may be used for assertions.
      Parameters:
      check - assertion callback to execute after exit call
      Returns:
      activated rule
    • checkAfterExit

      In junit it is impossible to use assertion lines after System.exit() call. This method will register a custom check callback to validate state after system exit call. May be called multiple times.

      NOTE: in spock there is no need for it, because 'then' section will be called.

      Parameters:
      check - assertion command
      Returns:
      rule instance for chained calls
      See Also:
      • ExpectedSystemExit.checkAssertionAfterwards(org.junit.contrib.java.lang.system.Assertion)
    • apply

      public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
      Specified by:
      apply in interface org.junit.rules.TestRule
    • getOutput

      public String getOutput()
      NOTE: useful only for spock tests.
      Returns:
      content of system.out or empty string
    • getError

      public String getError()
      Dropwizard exception will be presented here.

      NOTE: useful only for spock tests.

      Returns:
      content of system.err or empty string
    • getIndicatorExceptionType

      public Class<? extends Exception> getIndicatorExceptionType()
      Useful only for spock tests because in junit rule can intercept this exception implicitly.
      Returns:
      type of exception thrown when exit called