Package ru.vyarus.dropwizard.guice.test
Class StartupErrorRule
java.lang.Object
ru.vyarus.dropwizard.guice.test.StartupErrorRule
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface implementation may be registered to check assertions after system exit. -
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description) 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 StartupErrorRuleThis is useful for junit tests, because there is no other way to check anything after exit call.getError()Dropwizard exception will be presented here.Useful only for spock tests because in junit rule can intercept this exception implicitly.NOTE: useful only for spock tests.
-
Method Details
-
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 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
NOTE: useful only for spock tests.- Returns:
- content of system.out or empty string
-
getError
Dropwizard exception will be presented here.NOTE: useful only for spock tests.
- Returns:
- content of system.err or empty string
-
getIndicatorExceptionType
Useful only for spock tests because in junit rule can intercept this exception implicitly.- Returns:
- type of exception thrown when exit called
-