Skip to content

Commit 36d4cfa

Browse files
committed
fix(java-dsl): Now triggers can throw AssertionErrors if they wish to fail the test, regardless of whether an Exception is expected from the trigger or not
1 parent 980ce97 commit 36d4cfa

3 files changed

Lines changed: 87 additions & 3 deletions

File tree

packages/dsl-java/src/main/java/io/contract_testing/contractcase/internal/BoundaryTriggerMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public static <T> ITriggerFunction map(
2727
T ret;
2828
try {
2929
ret = trigger.call(interactionSetup);
30+
} catch (AssertionError error) {
31+
return ConnectorExceptionMapper.mapAsTriggerFailure(error);
3032
} catch (Exception e) {
3133
return ConnectorExceptionMapper.mapAsTriggerFailure(e);
3234
}
@@ -58,6 +60,8 @@ public static <T> ITriggerFunction map(Trigger<T> trigger,
5860
new RuntimeException(
5961
"Expected the trigger to throw an exception, but it returned successfully. Result toString() was: "
6062
+ unexpectedResult.toString()));
63+
} catch (AssertionError error) {
64+
return ConnectorExceptionMapper.mapAsTriggerFailure(error);
6165
} catch (Exception triggerException) {
6266
try {
6367
testErrorResponseFunction.call(triggerException, interactionSetup);

packages/dsl-java/src/main/java/io/contract_testing/contractcase/internal/edge/ConnectorExceptionMapper.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public static ConnectorFailure mapAsTriggerFailure(Exception e) {
5555
);
5656
}
5757

58+
public static ConnectorFailure mapAsTriggerFailure(Error e) {
59+
return new ConnectorFailure(
60+
ConnectorFailureKindConstants.CASE_TRIGGER_ERROR,
61+
e.getMessage(),
62+
"",
63+
"UNDOCUMENTED",
64+
stackTraceToString(e)
65+
);
66+
}
67+
5868
public static ConnectorFailure mapAsVerifyFailure(Exception e) {
5969
return new ConnectorFailure(
6070
ConnectorFailureKindConstants.CASE_VERIFY_RETURN_ERROR,

packages/dsl-java/src/test/java/io/contract_testing/contractcase/test/FailingExampleTest.java

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class FailingExampleTest {
5151

5252
@Test
5353
public void failingTrigger() {
54-
// failure in runInteraction trigger should return configuration error
54+
// exception in runInteraction trigger should return configuration error
5555
assertThatThrownBy(() -> {
5656
contract.runInteraction(
5757
new InteractionDefinition<>(
@@ -83,8 +83,8 @@ public void failingTrigger() {
8383
}
8484
);
8585
;
86-
// Failure in throwing interaction trigger should fail with verify error, as the
87-
// trigger's failure is passed to it
86+
// exception in throwing interaction trigger should fail with verify error, as the
87+
// trigger's failure is passed to the verify method, which should fail the assertion.
8888
assertThatThrownBy(() -> {
8989
contract.runThrowingInteraction(
9090
new InteractionDefinition<>(
@@ -115,6 +115,39 @@ public void failingTrigger() {
115115
}
116116
);
117117

118+
// Assertion error in failure trigger should fail the test with configuration error
119+
assertThatThrownBy(() -> {
120+
contract.runThrowingInteraction(
121+
new InteractionDefinition<>(
122+
List.of(new InState("Server is broken")),
123+
new WillSendHttpRequest(HttpExample.builder()
124+
125+
.request(new NamedMatch(
126+
"Get health",
127+
new HttpRequest(HttpRequestExample.builder()
128+
.path("/health")
129+
.method("GET")
130+
.build())
131+
))
132+
.response(new HttpResponse(HttpResponseExample.builder().status(503).build()))
133+
.build())
134+
),
135+
IndividualFailedTestConfigBuilder.<String>builder()
136+
.withProviderName("Java Example HTTP Server")
137+
.withTrigger((interactionSetup) -> {
138+
throw new AssertionError("This is meant to fail");
139+
})
140+
.withTestErrorResponse((exception, setupInfo) -> {
141+
assertThat(exception.getMessage()).isEqualTo("The server is not ready");
142+
})
143+
);
144+
}).isInstanceOf(ContractCaseConfigurationError.class)
145+
.satisfies((e) -> {
146+
assertThat(((HasUserFacingStackTrace) e).userFacingStackTrace())
147+
.contains("FailingExampleTest.java");
148+
}
149+
);
150+
118151
assertThatThrownBy(() -> {
119152
contract.runInteraction(
120153
new InteractionDefinition<>(
@@ -148,6 +181,43 @@ public void failingTrigger() {
148181
}
149182
);
150183

184+
// Assertion error in success trigger should fail with configuration error
185+
assertThatThrownBy(() -> {
186+
contract.runInteraction(
187+
new InteractionDefinition<>(
188+
List.of(new InState("Server is up")),
189+
new WillSendHttpRequest(HttpExample.builder()
190+
191+
.request(new NamedMatch(
192+
"Get health",
193+
new HttpRequest(HttpRequestExample.builder()
194+
.path("/health")
195+
.method("GET")
196+
.build())
197+
))
198+
.response(new HttpResponse(HttpResponseExample.builder()
199+
.status(200)
200+
.body(Map.ofEntries(Map.entry("status", "up")))
201+
.build()))
202+
.build())
203+
),
204+
IndividualSuccessTestConfigBuilder.<String>builder()
205+
.withProviderName("Java Example HTTP Server")
206+
.withTrigger((interactionSetup) -> {
207+
throw new AssertionError("This is meant to fail");
208+
})
209+
.withTestResponse((data, setupInfo) -> {
210+
assertThat(data).isEqualTo("It doesn't equal this");
211+
})
212+
);
213+
}).isInstanceOf(ContractCaseConfigurationError.class)
214+
.satisfies((e) -> {
215+
assertThat(((HasUserFacingStackTrace) e).userFacingStackTrace())
216+
.contains("FailingExampleTest.java");
217+
}
218+
);
219+
220+
151221
// Recording contract should throw an error
152222
assertThatThrownBy(contract::endRecord).
153223

0 commit comments

Comments
 (0)