Package ru.vyarus.dropwizard.guice.test
Class StartupErrorRule
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.StartupErrorRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class StartupErrorRule extends java.lang.Object implements org.junit.rules.TestRuleDropwizard 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(orthrown(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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceStartupErrorRule.AfterExitAssertionInterface implementation may be registered to check assertions after system exit.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description org.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description)StartupErrorRulecheckAfterExit(StartupErrorRule.AfterExitAssertion check)In junit it is impossible to use assertion lines afterSystem.exit()call.static StartupErrorRulecreate()Use with spock tests or when no assertions required after system exit call.static StartupErrorRulecreate(StartupErrorRule.AfterExitAssertion check)This is useful for junit tests, because there is no other way to check anything after exit call.java.lang.StringgetError()Dropwizard exception will be presented here.java.lang.Class<? extends java.lang.Exception>getIndicatorExceptionType()Useful only for spock tests because in junit rule can intercept this exception implicitly.java.lang.StringgetOutput()NOTE: useful only for spock tests.
-
-
-
Method Detail
-
create
public static StartupErrorRule create()
Use with spock tests or when no assertions required after system exit call.- Returns:
- rule instance
-
create
public static StartupErrorRule create(StartupErrorRule.AfterExitAssertion check)
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
public StartupErrorRule checkAfterExit(StartupErrorRule.AfterExitAssertion check)
In junit it is impossible to use assertion lines afterSystem.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:
applyin interfaceorg.junit.rules.TestRule
-
getOutput
public java.lang.String getOutput()
NOTE: useful only for spock tests.- Returns:
- content of system.out or empty string
-
getError
public java.lang.String getError()
Dropwizard exception will be presented here.NOTE: useful only for spock tests.
- Returns:
- content of system.err or empty string
-
getIndicatorExceptionType
public java.lang.Class<? extends java.lang.Exception> getIndicatorExceptionType()
Useful only for spock tests because in junit rule can intercept this exception implicitly.- Returns:
- type of exception thrown when exit called
-
-