|
1 | 1 | package org.hamcrest;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import javax.xml.stream.XMLStreamException; |
| 5 | + |
3 | 6 | import org.junit.Test;
|
4 | 7 |
|
| 8 | +import static org.hamcrest.Matchers.*; |
5 | 9 | import static org.hamcrest.MatcherAssert.assertThat;
|
6 |
| -import static org.hamcrest.core.IsEqual.equalTo; |
7 | 10 | import static org.junit.Assert.*;
|
8 | 11 |
|
9 | 12 | public final class MatcherAssertTest {
|
@@ -96,4 +99,157 @@ public void describeMismatch(Object item, Description mismatchDescription) {
|
96 | 99 | canAssertSubtypes() {
|
97 | 100 | assertThat(1, equalTo((Number) 1));
|
98 | 101 | }
|
| 102 | + |
| 103 | + @Test public void |
| 104 | + throwableIsOfMatchingInstance() { |
| 105 | + assertThat( |
| 106 | + new Executable() { |
| 107 | + @Override |
| 108 | + public void execute() { |
| 109 | + throw new IllegalStateException(); |
| 110 | + } |
| 111 | + }, |
| 112 | + throwsInstanceOf(IllegalStateException.class) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + @Test public void |
| 117 | + throwableIsNotOfMatchingInstance() { |
| 118 | + String endLine = System.lineSeparator(); |
| 119 | + String expectedMessage = endLine + "Expected: throws an instance of java.io.IOException" + endLine |
| 120 | + + " but: threw but <java.lang.IllegalStateException> is a java.lang.IllegalStateException"; |
| 121 | + try { |
| 122 | + assertThat( |
| 123 | + new Executable() { |
| 124 | + @Override |
| 125 | + public void execute() { |
| 126 | + throw new IllegalStateException(); |
| 127 | + } |
| 128 | + }, |
| 129 | + throwsInstanceOf(IOException.class) |
| 130 | + ); |
| 131 | + fail("should have failed"); |
| 132 | + } |
| 133 | + catch (AssertionError e) { |
| 134 | + assertEquals(expectedMessage, e.getMessage()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test public void |
| 139 | + throwableHasMatchingMessage() { |
| 140 | + assertThat( |
| 141 | + new Executable() { |
| 142 | + @Override |
| 143 | + public void execute() throws Exception { |
| 144 | + throw new Exception("message"); |
| 145 | + } |
| 146 | + }, |
| 147 | + doesThrow(withMessage(equalTo("message"))) |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + @Test public void |
| 152 | + throwableDoesNotHaveMatchingMessage() { |
| 153 | + String endLine = System.lineSeparator(); |
| 154 | + String expectedMessage = endLine + "Expected: throws with message \"expected message\"" + endLine |
| 155 | + + " but: threw but message was \"actual message\""; |
| 156 | + try { |
| 157 | + assertThat( |
| 158 | + new Executable() { |
| 159 | + @Override |
| 160 | + public void execute() throws Exception { |
| 161 | + throw new Exception("actual message"); |
| 162 | + } |
| 163 | + }, |
| 164 | + doesThrow(withMessage("expected message")) |
| 165 | + ); |
| 166 | + fail("should have failed"); |
| 167 | + } |
| 168 | + catch (AssertionError e) { |
| 169 | + assertEquals(expectedMessage, e.getMessage()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Test public void |
| 174 | + throwableExecutionDoesNotThrow() { |
| 175 | + String endLine = System.lineSeparator(); |
| 176 | + String expectedMessage = endLine + "Expected: throws an instance of java.lang.NoSuchMethodError" |
| 177 | + + endLine + " but: did not throw"; |
| 178 | + try { |
| 179 | + assertThat( |
| 180 | + new Executable() { |
| 181 | + @Override |
| 182 | + public void execute() { |
| 183 | + // Do nothing |
| 184 | + } |
| 185 | + }, |
| 186 | + throwsInstanceOf(NoSuchMethodError.class) |
| 187 | + ); |
| 188 | + fail("should have failed"); |
| 189 | + } |
| 190 | + catch (AssertionError e) { |
| 191 | + assertEquals(expectedMessage, e.getMessage()); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @Test public void |
| 196 | + throwableCauseMatches() { |
| 197 | + Matcher<? extends Throwable> instanceOfMatcher = instanceOf(XMLStreamException.class); |
| 198 | + assertThat( |
| 199 | + new Executable() { |
| 200 | + @Override |
| 201 | + public void execute() { |
| 202 | + throw new RuntimeException(new XMLStreamException()); |
| 203 | + } |
| 204 | + }, |
| 205 | + doesThrow(becauseOf(instanceOfMatcher)) |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + @Test public void |
| 210 | + throwableCauseDoesNotMatch() { |
| 211 | + String endLine = System.lineSeparator(); |
| 212 | + String expectedMessage = endLine + "Expected: throws because of an instance of java.lang.NullPointerException" |
| 213 | + + endLine + " but: threw but cause <java.lang.IllegalArgumentException> is a java.lang.IllegalArgumentException"; |
| 214 | + try { |
| 215 | + Matcher<? extends Throwable> instanceOfMatcher = instanceOf(NullPointerException.class); |
| 216 | + assertThat( |
| 217 | + new Executable() { |
| 218 | + @Override |
| 219 | + public void execute() { |
| 220 | + throw new RuntimeException(new IllegalArgumentException()); |
| 221 | + } |
| 222 | + }, |
| 223 | + doesThrow(becauseOf(instanceOfMatcher)) |
| 224 | + ); |
| 225 | + fail("should have failed"); |
| 226 | + } |
| 227 | + catch (AssertionError e) { |
| 228 | + assertEquals(expectedMessage, e.getMessage()); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + @Test public void |
| 233 | + throwableExecutionDoesNotMatchWithCustomMessage() { |
| 234 | + String endLine = System.lineSeparator(); |
| 235 | + String expectedMessage = "Custom message" |
| 236 | + + endLine + "Expected: throws an instance of java.lang.NullPointerException" |
| 237 | + + endLine + " but: threw but <java.lang.IllegalArgumentException> is a java.lang.IllegalArgumentException"; |
| 238 | + try { |
| 239 | + assertThat( |
| 240 | + "Custom message", |
| 241 | + new Executable() { |
| 242 | + @Override |
| 243 | + public void execute() { |
| 244 | + throw new IllegalArgumentException(); |
| 245 | + } |
| 246 | + }, |
| 247 | + throwsInstanceOf(NullPointerException.class) |
| 248 | + ); |
| 249 | + fail("should have failed"); |
| 250 | + } |
| 251 | + catch (AssertionError e) { |
| 252 | + assertEquals(expectedMessage, e.getMessage()); |
| 253 | + } |
| 254 | + } |
99 | 255 | }
|
0 commit comments