Skip to content

Commit e801a7c

Browse files
matchers: code throw exception value matcher (#1544)
1 parent 3d2a826 commit e801a7c

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

webtau-core/src/main/java/org/testingisdocumenting/webtau/Matchers.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ public static ThrowExceptionMatcher throwException(String expectedMessage) {
293293
return new ThrowExceptionMatcher(expectedMessage);
294294
}
295295

296+
/**
297+
* Throw exception <code>code</code> matcher.
298+
* <pre>
299+
* code(() -&gt; {
300+
* businessLogic(-10);
301+
* }).should(throwException(contain("negatives are not")));
302+
* </pre>
303+
* @see #code(CodeBlock)
304+
*
305+
* @param expectedMessageMatcher expected exception message ValueMatcher
306+
* @return matcher instance
307+
*/
308+
public static ThrowExceptionMatcher throwException(ValueMatcher expectedMessageMatcher) {
309+
return new ThrowExceptionMatcher(expectedMessageMatcher);
310+
}
311+
296312
/**
297313
* Throw exception <code>code</code> matcher.
298314
* <pre>

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/code/ThrowExceptionMatcher.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717

1818
package org.testingisdocumenting.webtau.expectation.code;
1919

20-
import org.testingisdocumenting.webtau.expectation.ActualValueAware;
21-
import org.testingisdocumenting.webtau.expectation.CodeBlock;
22-
import org.testingisdocumenting.webtau.expectation.CodeMatcher;
23-
import org.testingisdocumenting.webtau.expectation.ExpectedValuesAware;
20+
import org.testingisdocumenting.webtau.expectation.*;
2421
import org.testingisdocumenting.webtau.expectation.equality.CompareToComparator;
2522
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
2623
import org.testingisdocumenting.webtau.reporter.stacktrace.StackTraceUtils;
@@ -34,20 +31,23 @@
3431
import static org.testingisdocumenting.webtau.WebTauCore.*;
3532

3633
public class ThrowExceptionMatcher implements CodeMatcher, ExpectedValuesAware, ActualValueAware {
37-
private String expectedMessage;
38-
private Pattern expectedMessageRegexp;
34+
private Object expectedMessageMatcherOrValue;
3935
private Class<?> expectedClass;
4036
private String thrownMessage;
4137
private Class<?> thrownClass;
4238
private CompareToComparator comparator;
4339
private String thrownExceptionStackTrace;
4440

4541
public ThrowExceptionMatcher(String expectedMessage) {
46-
this.expectedMessage = expectedMessage;
42+
this.expectedMessageMatcherOrValue = expectedMessage;
43+
}
44+
45+
public ThrowExceptionMatcher(ValueMatcher expectedMessageMatcher) {
46+
this.expectedMessageMatcherOrValue = expectedMessageMatcher;
4747
}
4848

4949
public ThrowExceptionMatcher(Pattern expectedMessageRegexp) {
50-
this.expectedMessageRegexp = expectedMessageRegexp;
50+
this.expectedMessageMatcherOrValue = expectedMessageRegexp;
5151
}
5252

5353
public ThrowExceptionMatcher(Class<?> expectedClass) {
@@ -56,12 +56,12 @@ public ThrowExceptionMatcher(Class<?> expectedClass) {
5656

5757
public ThrowExceptionMatcher(Class<?> expectedClass, Pattern expectedMessageRegexp) {
5858
this.expectedClass = expectedClass;
59-
this.expectedMessageRegexp = expectedMessageRegexp;
59+
this.expectedMessageMatcherOrValue = expectedMessageRegexp;
6060
}
6161

6262
public ThrowExceptionMatcher(Class<?> expectedClass, String expectedMessage) {
6363
this.expectedClass = expectedClass;
64-
this.expectedMessage = expectedMessage;
64+
this.expectedMessageMatcherOrValue = expectedMessage;
6565
}
6666

6767
@Override
@@ -90,19 +90,15 @@ public TokenizedMessage mismatchedTokenizedMessage(CodeBlock codeBlock) {
9090

9191
@Override
9292
public Stream<Object> expectedValues() {
93-
if (expectedMessage != null && expectedClass != null) {
94-
return Stream.of(expectedClass, expectedMessage);
93+
if (expectedMessageMatcherOrValue != null && expectedClass != null) {
94+
return Stream.of(expectedClass, expectedMessageMatcherOrValue);
9595
}
9696

9797
if (expectedClass != null) {
9898
return Stream.of(expectedClass);
9999
}
100100

101-
if (expectedMessage != null) {
102-
return Stream.of(expectedMessage);
103-
}
104-
105-
return Stream.empty();
101+
return Stream.ofNullable(expectedMessageMatcherOrValue);
106102
}
107103

108104
@Override
@@ -128,7 +124,7 @@ public boolean matches(CodeBlock codeBlock) {
128124

129125
private Map<String, Object> buildThrownToUseForCompare() {
130126
Map<String, Object> result = new HashMap<>();
131-
if (expectedMessage != null || expectedMessageRegexp != null) {
127+
if (expectedMessageMatcherOrValue != null) {
132128
result.put("message", thrownMessage);
133129
}
134130

@@ -141,12 +137,8 @@ private Map<String, Object> buildThrownToUseForCompare() {
141137

142138
private Map<String, Object> buildExpectedMapToUseForCompare() {
143139
Map<String, Object> result = new HashMap<>();
144-
if (expectedMessage != null) {
145-
result.put("message", expectedMessage);
146-
}
147-
148-
if (expectedMessageRegexp != null) {
149-
result.put("message", expectedMessageRegexp);
140+
if (expectedMessageMatcherOrValue != null) {
141+
result.put("message", expectedMessageMatcherOrValue);
150142
}
151143

152144
if (expectedClass != null) {

webtau-core/src/test/groovy/org/testingisdocumenting/webtau/expectation/code/ThrowExceptionMatcherGroovyTest.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ class ThrowExceptionMatcherGroovyTest {
4848
}
4949
}
5050

51+
@Test
52+
void "should validate exception using contain matcher mismatch case"() {
53+
runExpectExceptionAndValidateOutput(AssertionError, contain('> expecting code to throw exception <contain "message1">\n' +
54+
'X failed expecting code to throw exception <contain "message1">:\n' +
55+
' exception.message: no match found')) {
56+
code {
57+
throw new RuntimeException('error message')
58+
} should throwException(contain('message1'))
59+
}
60+
}
61+
62+
@Test
63+
void "should validate exception using contain matcher match case"() {
64+
runAndValidateOutput('> expecting code to throw exception <contain "message">\n' +
65+
'. code thrown <contain "message"> (Xms)') {
66+
code {
67+
throw new RuntimeException('error message')
68+
} should throwException(contain('message'))
69+
}
70+
}
71+
5172
@Test
5273
void "should validate exception class"() {
5374
runExpectExceptionAndValidateOutput(AssertionError, contain('X failed expecting code to throw exception java.lang.UnsupportedOperationException:\n' +

0 commit comments

Comments
 (0)