Skip to content

Commit 4bf309e

Browse files
matchers: code throw exception value matcher with class (#1547)
1 parent 5093475 commit 4bf309e

4 files changed

Lines changed: 40 additions & 12 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,21 @@ public static ThrowExceptionMatcher throwException(Class<?> expectedClass, Patte
374374
public static ThrowExceptionMatcher throwException(Class<?> expectedClass, String expectedMessage) {
375375
return new ThrowExceptionMatcher(expectedClass, expectedMessage);
376376
}
377+
378+
/**
379+
* Throw exception <code>code</code> matcher.
380+
* <pre>
381+
* code(() -&gt; {
382+
* businessLogic(-10);
383+
* }).should(throwException(IllegalArgumentException.class, contain("negatives are not")));
384+
* </pre>
385+
* @see #code(CodeBlock)
386+
*
387+
* @param expectedClass expected exception class
388+
* @param expectedMessageMatcher expected exception message ValueMatcher
389+
* @return matcher instance
390+
*/
391+
public static ThrowExceptionMatcher throwException(Class<?> expectedClass, ValueMatcher expectedMessageMatcher) {
392+
return new ThrowExceptionMatcher(expectedClass, expectedMessageMatcher);
393+
}
377394
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public ThrowExceptionMatcher(Class<?> expectedClass, String expectedMessage) {
6464
this.expectedMessageMatcherOrValue = expectedMessage;
6565
}
6666

67+
public ThrowExceptionMatcher(Class<?> expectedClass, ValueMatcher expectedMessageMatcher) {
68+
this.expectedClass = expectedClass;
69+
this.expectedMessageMatcherOrValue = expectedMessageMatcher;
70+
}
71+
6772
@Override
6873
public TokenizedMessage matchingTokenizedMessage() {
6974
return tokenizedMessage().matcher("to throw exception").value(buildExpectedValueForMessage());
@@ -123,26 +128,21 @@ public boolean matches(CodeBlock codeBlock) {
123128
}
124129

125130
private Map<String, Object> buildThrownToUseForCompare() {
126-
Map<String, Object> result = new HashMap<>();
127-
if (expectedMessageMatcherOrValue != null) {
128-
result.put("message", thrownMessage);
129-
}
130-
131-
if (expectedClass != null) {
132-
result.put("class", thrownClass);
133-
}
134-
135-
return result;
131+
return createMap(thrownMessage, thrownClass);
136132
}
137133

138134
private Map<String, Object> buildExpectedMapToUseForCompare() {
135+
return createMap(expectedMessageMatcherOrValue, expectedClass);
136+
}
137+
138+
private Map<String, Object> createMap(Object message, Class<?> aClass) {
139139
Map<String, Object> result = new HashMap<>();
140140
if (expectedMessageMatcherOrValue != null) {
141-
result.put("message", expectedMessageMatcherOrValue);
141+
result.put("message", message);
142142
}
143143

144144
if (expectedClass != null) {
145-
result.put("class", expectedClass);
145+
result.put("class", aClass);
146146
}
147147

148148
return result;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ class ThrowExceptionMatcherGroovyTest {
107107
}
108108
}
109109

110+
@Test
111+
void "should validate exception using class and contain matcher mismatch case"() {
112+
runAndValidateOutput("> expecting code to throw exception {\"message\": <contain \"message\">, \"class\": java.lang.IllegalArgumentException}\n" +
113+
". code thrown {\"message\": <contain \"message\">, \"class\": java.lang.IllegalArgumentException} (Xms)") {
114+
code {
115+
throw new IllegalArgumentException('error message')
116+
} should throwException(IllegalArgumentException.class, contain('message'))
117+
}
118+
}
119+
110120
@Test
111121
void "should add exception stack trace when mismatched"() {
112122
runExpectExceptionAndValidateOutput(AssertionError.class, contain("stack trace:\n" +
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: `throwException` `code` matcher accepts `ValueMatchers` in addition to `String` and `Pattern`

0 commit comments

Comments
 (0)