|
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,115 @@ 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 | + () -> { throw new IllegalStateException(); }, |
| 107 | + throwsInstanceOf(IllegalStateException.class) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + @Test public void |
| 112 | + throwableIsNotOfMatchingInstance() { |
| 113 | + String endLine = System.lineSeparator(); |
| 114 | + String expectedMessage = endLine + "Expected: throws an instance of java.io.IOException" + endLine |
| 115 | + + " but: threw but <java.lang.IllegalStateException> is a java.lang.IllegalStateException"; |
| 116 | + try { |
| 117 | + assertThat( |
| 118 | + () -> { throw new IllegalStateException(); }, |
| 119 | + throwsInstanceOf(IOException.class) |
| 120 | + ); |
| 121 | + fail("should have failed"); |
| 122 | + } |
| 123 | + catch (AssertionError e) { |
| 124 | + assertEquals(expectedMessage, e.getMessage()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test public void |
| 129 | + throwableHasMatchingMessage() { |
| 130 | + assertThat( |
| 131 | + () -> { throw new Exception("message"); }, |
| 132 | + doesThrow(withMessage(equalTo("message"))) |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + @Test public void |
| 137 | + throwableDoesNotHaveMatchingMessage() { |
| 138 | + String endLine = System.lineSeparator(); |
| 139 | + String expectedMessage = endLine + "Expected: throws with message \"expected message\"" + endLine |
| 140 | + + " but: threw but message was \"actual message\""; |
| 141 | + try { |
| 142 | + assertThat( |
| 143 | + () -> { throw new Exception("actual message"); }, |
| 144 | + doesThrow(withMessage("expected message")) |
| 145 | + ); |
| 146 | + fail("should have failed"); |
| 147 | + } |
| 148 | + catch (AssertionError e) { |
| 149 | + assertEquals(expectedMessage, e.getMessage()); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test public void |
| 154 | + throwableExecutionDoesNotThrow() { |
| 155 | + String endLine = System.lineSeparator(); |
| 156 | + String expectedMessage = endLine + "Expected: throws an instance of java.lang.NoSuchMethodError" |
| 157 | + + endLine + " but: did not throw"; |
| 158 | + try { |
| 159 | + assertThat( |
| 160 | + () -> {}, // Do nothing |
| 161 | + throwsInstanceOf(NoSuchMethodError.class) |
| 162 | + ); |
| 163 | + fail("should have failed"); |
| 164 | + } |
| 165 | + catch (AssertionError e) { |
| 166 | + assertEquals(expectedMessage, e.getMessage()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Test public void |
| 171 | + throwableCauseMatches() { |
| 172 | + assertThat( |
| 173 | + () -> { throw new RuntimeException(new XMLStreamException()); }, |
| 174 | + doesThrow(becauseOf(instanceOf(XMLStreamException.class))) |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + @Test public void |
| 179 | + throwableCauseDoesNotMatch() { |
| 180 | + String endLine = System.lineSeparator(); |
| 181 | + String expectedMessage = endLine + "Expected: throws because of an instance of java.lang.NullPointerException" |
| 182 | + + endLine + " but: threw but cause <java.lang.IllegalArgumentException> is a java.lang.IllegalArgumentException"; |
| 183 | + try { |
| 184 | + assertThat( |
| 185 | + () -> { throw new RuntimeException(new IllegalArgumentException()); }, |
| 186 | + doesThrow(becauseOf(instanceOf(NullPointerException.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 | + throwableExecutionDoesNotMatchWithCustomMessage() { |
| 197 | + String endLine = System.lineSeparator(); |
| 198 | + String expectedMessage = "Custom message" |
| 199 | + + endLine + "Expected: throws an instance of java.lang.NullPointerException" |
| 200 | + + endLine + " but: threw but <java.lang.IllegalArgumentException> is a java.lang.IllegalArgumentException"; |
| 201 | + try { |
| 202 | + assertThat( |
| 203 | + "Custom message", |
| 204 | + () -> { throw new IllegalArgumentException(); }, |
| 205 | + throwsInstanceOf(NullPointerException.class) |
| 206 | + ); |
| 207 | + fail("should have failed"); |
| 208 | + } |
| 209 | + catch (AssertionError e) { |
| 210 | + assertEquals(expectedMessage, e.getMessage()); |
| 211 | + } |
| 212 | + } |
99 | 213 | } |
0 commit comments