forked from pact-foundation/pact-jvm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/v3.6.x'
- Loading branch information
Showing
3 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...provider-junit/src/test/groovy/au/com/dius/pact/provider/junit/RunStateChangesSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package au.com.dius.pact.provider.junit | ||
|
||
import au.com.dius.pact.core.model.ProviderState | ||
import kotlin.Pair | ||
import org.junit.runners.model.FrameworkMethod | ||
import org.junit.runners.model.Statement | ||
import spock.lang.Specification | ||
|
||
import java.util.function.Supplier | ||
|
||
@SuppressWarnings(['ThrowRuntimeException', 'UnnecessaryParenthesesForMethodCallWithClosure']) | ||
class RunStateChangesSpec extends Specification { | ||
|
||
private Statement next | ||
private ProviderState providerState | ||
private Map testContext | ||
private List<Pair<FrameworkMethod, State>> methods | ||
private List<Supplier> stateChangeHandlers | ||
|
||
class TestTarget { | ||
boolean called = false | ||
boolean teardownCalled = false | ||
|
||
@State('Test State') | ||
void stateChange() { | ||
called = true | ||
} | ||
|
||
@State(value = 'Test State', action = StateChangeAction.TEARDOWN) | ||
void stateChangeTeardown() { | ||
teardownCalled = true | ||
} | ||
} | ||
|
||
private TestTarget target | ||
|
||
def setup() { | ||
providerState = new ProviderState('Test State') | ||
testContext = [:] | ||
next = Mock() | ||
methods = [ | ||
new Pair(new FrameworkMethod(TestTarget.getDeclaredMethod('stateChange')), | ||
TestTarget.getDeclaredMethod('stateChange').getAnnotation(State)) | ||
] | ||
stateChangeHandlers = [ | ||
{ target } as Supplier | ||
] | ||
target = Spy(TestTarget) | ||
} | ||
|
||
def 'invokes the state change method before the next statement'() { | ||
when: | ||
new RunStateChanges(next, methods, stateChangeHandlers, providerState, testContext).evaluate() | ||
|
||
then: | ||
1 * next.evaluate() | ||
1 * target.stateChange() | ||
0 * target.stateChangeTeardown() | ||
} | ||
|
||
def 'invokes the state change teardown method after the next statement'() { | ||
given: | ||
methods << new Pair(new FrameworkMethod(TestTarget.getDeclaredMethod('stateChangeTeardown')), | ||
TestTarget.getDeclaredMethod('stateChangeTeardown').getAnnotation(State)) | ||
|
||
when: | ||
new RunStateChanges(next, methods, stateChangeHandlers, providerState, testContext).evaluate() | ||
|
||
then: | ||
1 * next.evaluate() | ||
1 * target.stateChange() | ||
|
||
then: | ||
1 * target.stateChangeTeardown() | ||
} | ||
|
||
def 'still invokes the state change teardown method if the the next statement fails'() { | ||
given: | ||
methods << new Pair(new FrameworkMethod(TestTarget.getDeclaredMethod('stateChangeTeardown')), | ||
TestTarget.getDeclaredMethod('stateChangeTeardown').getAnnotation(State)) | ||
next = Mock() { | ||
evaluate() >> { throw new RuntimeException('Boom') } | ||
} | ||
|
||
when: | ||
new RunStateChanges(next, methods, stateChangeHandlers, providerState, testContext).evaluate() | ||
|
||
then: | ||
1 * target.stateChange() | ||
thrown(RuntimeException) | ||
|
||
then: | ||
1 * target.stateChangeTeardown() | ||
} | ||
|
||
} |