@@ -273,6 +273,73 @@ void should_not_throw_exception_when_map_entry_missing() {
273
273
thenNoException ().isThrownBy (() -> assertThat (context ).doesNotHaveMapEntry ("foo" , "bar" ));
274
274
}
275
275
276
+ @ Test
277
+ void should_not_throw_when_does_not_have_error () {
278
+ thenNoException ().isThrownBy (() -> assertThat (context ).doesNotHaveError ());
279
+ }
280
+
281
+ @ Test
282
+ void should_throw_when_unexpected_error () {
283
+ Throwable expected = new IllegalStateException ("test" );
284
+
285
+ registry .observationConfig ().observationHandler (c -> true );
286
+ Observation observation = Observation .start ("foo" , context , registry );
287
+ observation .error (expected );
288
+
289
+ thenThrownBy (() -> assertThat (context ).doesNotHaveError ())
290
+ .hasMessage ("Observation should not have an error, found <java.lang.IllegalStateException: test>" );
291
+ }
292
+
293
+ @ Test
294
+ void should_not_throw_when_has_error () {
295
+ Throwable expected = new IllegalStateException ("test" );
296
+
297
+ registry .observationConfig ().observationHandler (c -> true );
298
+ Observation observation = Observation .start ("foo" , context , registry );
299
+ observation .error (expected );
300
+
301
+ thenNoException ().isThrownBy (() -> assertThat (context ).hasError ());
302
+ }
303
+
304
+ @ Test
305
+ void should_throw_when_has_error_missing () {
306
+ thenThrownBy (() -> assertThat (context ).hasError ())
307
+ .hasMessage ("Observation should have an error, but none was found" );
308
+ }
309
+
310
+ @ Test
311
+ void should_not_throw_when_has_specific_error () {
312
+ Throwable expected = new IllegalStateException ("test" );
313
+
314
+ registry .observationConfig ().observationHandler (c -> true );
315
+ Observation observation = Observation .start ("foo" , context , registry );
316
+ observation .error (expected );
317
+
318
+ thenNoException ().isThrownBy (() -> assertThat (context ).hasError (expected ));
319
+ }
320
+
321
+ @ Test
322
+ void should_throw_when_has_specific_error_missing () {
323
+ Throwable expected = new IllegalStateException ("test" );
324
+
325
+ thenThrownBy (() -> assertThat (context ).hasError (expected ))
326
+ .hasMessage ("Observation should have an error, but none was found" );
327
+ }
328
+
329
+ @ Test
330
+ void should_throw_when_has_specific_error_does_not_match () {
331
+ Throwable expected = new IllegalStateException ("test expected" );
332
+ Throwable actual = new IllegalArgumentException ("test actual" );
333
+
334
+ registry .observationConfig ().observationHandler (c -> true );
335
+ Observation observation = Observation .start ("foo" , context , registry );
336
+ observation .error (actual );
337
+
338
+ thenThrownBy (() -> assertThat (context ).hasError (expected ))
339
+ .hasMessage ("Observation expected to have error <java.lang.IllegalStateException: test expected>,"
340
+ + " but has <java.lang.IllegalArgumentException: test actual>" );
341
+ }
342
+
276
343
@ Test
277
344
void should_jump_to_and_back_from_throwable_assert () {
278
345
context .setName ("foo" ).setError (new RuntimeException ("bar" ));
0 commit comments