@@ -284,89 +284,61 @@ void testGenerationException1() {
284284 StringWriter writer = new StringWriter ();
285285 JsonGenerator generator = Json .createGenerator (writer );
286286 generator .writeStartObject ();
287- try {
288- generator .writeStartObject ();
289- Assertions .fail ("Expected JsonGenerationException, writeStartObject() cannot be called more than once" );
290- } catch (JsonGenerationException je ) {
291- // Expected exception
292- }
287+ Assertions .assertThrows (JsonGenerationException .class , generator ::writeStartObject ,
288+ "Expected JsonGenerationException, writeStartObject() cannot be called more than once" );
293289 }
294290
295291 @ Test
296292 void testGenerationException2 () {
297293 StringWriter writer = new StringWriter ();
298294 JsonGenerator generator = Json .createGenerator (writer );
299295 generator .writeStartObject ();
300- try {
301- generator .writeStartArray ();
302- Assertions .fail ("Expected JsonGenerationException, writeStartArray() is valid in no context" );
303- } catch (JsonGenerationException je ) {
304- // Expected exception
305- }
296+ Assertions .assertThrows (JsonGenerationException .class , generator ::writeStartArray ,
297+ "Expected JsonGenerationException, writeStartArray() is valid in no context" );
306298 }
307299
308300 @ Test
309301 void testGenerationException3 () {
310302 StringWriter writer = new StringWriter ();
311303 JsonGenerator generator = Json .createGenerator (writer );
312- try {
313- generator .close ();
314- Assertions .fail ("Expected JsonGenerationException, no JSON is generated" );
315- } catch (JsonGenerationException je ) {
316- // Expected exception
317- }
304+ Assertions .assertThrows (JsonGenerationException .class , generator ::close ,
305+ "Expected JsonGenerationException, no JSON is generated" );
318306 }
319307
320308 @ Test
321309 void testGenerationException4 () {
322310 StringWriter writer = new StringWriter ();
323311 JsonGenerator generator = Json .createGenerator (writer );
324312 generator .writeStartArray ();
325- try {
326- generator .close ();
327- Assertions .fail ("Expected JsonGenerationException, writeEnd() is not called" );
328- } catch (JsonGenerationException je ) {
329- // Expected exception
330- }
313+ Assertions .assertThrows (JsonGenerationException .class , generator ::close ,
314+ "Expected JsonGenerationException, writeEnd() is not called" );
331315 }
332316
333317 @ Test
334318 void testGenerationException5 () {
335319 StringWriter writer = new StringWriter ();
336320 JsonGenerator generator = Json .createGenerator (writer );
337321 generator .writeStartObject ();
338- try {
339- generator .close ();
340- Assertions .fail ("Expected JsonGenerationException, writeEnd() is not called" );
341- } catch (JsonGenerationException je ) {
342- // Expected exception
343- }
322+ Assertions .assertThrows (JsonGenerationException .class , generator ::close ,
323+ "Expected JsonGenerationException, writeEnd() is not called" );
344324 }
345325
346326 @ Test
347327 void testGenerationException6 () {
348328 StringWriter writer = new StringWriter ();
349329 JsonGenerator generator = Json .createGenerator (writer );
350330 generator .writeStartObject ().writeEnd ();
351- try {
352- generator .writeStartObject ();
353- Assertions .fail ("Expected JsonGenerationException, cannot generate one more JSON text" );
354- } catch (JsonGenerationException je ) {
355- // Expected exception
356- }
331+ Assertions .assertThrows (JsonGenerationException .class , generator ::writeStartObject ,
332+ "Expected JsonGenerationException, cannot generate one more JSON text" );
357333 }
358334
359335 @ Test
360336 void testGenerationException7 () {
361337 StringWriter writer = new StringWriter ();
362338 JsonGenerator generator = Json .createGenerator (writer );
363339 generator .writeStartArray ().writeEnd ();
364- try {
365- generator .writeStartArray ();
366- Assertions .fail ("Expected JsonGenerationException, cannot generate one more JSON text" );
367- } catch (JsonGenerationException je ) {
368- // Expected exception
369- }
340+ Assertions .assertThrows (JsonGenerationException .class , generator ::writeStartArray ,
341+ "Expected JsonGenerationException, cannot generate one more JSON text" );
370342 }
371343
372344
@@ -375,50 +347,33 @@ void testGenerationException8() {
375347 StringWriter sWriter = new StringWriter ();
376348 JsonGenerator generator = Json .createGenerator (sWriter );
377349 generator .writeStartObject ();
378- try {
379- generator .write (JsonValue .TRUE );
380- Assertions .fail ("Expected JsonGenerationException, cannot generate one more JSON text" );
381- } catch (JsonGenerationException je ) {
382- // Expected exception
383- }
350+ Assertions .assertThrows (JsonGenerationException .class , () -> generator .write (JsonValue .TRUE ),
351+ "Expected JsonGenerationException, cannot generate one more JSON text" );
384352 }
385353
386354 @ Test
387355 void testGenerationException9 () {
388356 StringWriter sWriter = new StringWriter ();
389357 JsonGenerator generator = Json .createGenerator (sWriter );
390358 generator .writeStartObject ();
391- try {
392- generator .write ("name" );
393- Assertions .fail ("Expected JsonGenerationException, cannot generate one more JSON text" );
394- } catch (JsonGenerationException je ) {
395- // Expected exception
396- }
359+ Assertions .assertThrows (JsonGenerationException .class , () -> generator .write ("name" ),
360+ "Expected JsonGenerationException, cannot generate one more JSON text" );
397361 }
398362
399363 @ Test
400364 void testGeneratorArrayDouble () {
401365 StringWriter writer = new StringWriter ();
402366 JsonGenerator generator = Json .createGenerator (writer );
403367 generator .writeStartArray ();
404- try {
405- generator .write (Double .NaN );
406- Assertions .fail ("JsonGenerator.write(Double.NaN) should produce NumberFormatException" );
407- } catch (NumberFormatException ne ) {
408- // expected
409- }
410- try {
411- generator .write (Double .POSITIVE_INFINITY );
412- Assertions .fail ("JsonGenerator.write(Double.POSITIVE_INIFINITY) should produce NumberFormatException" );
413- } catch (NumberFormatException ne ) {
414- // expected
415- }
416- try {
417- generator .write (Double .NEGATIVE_INFINITY );
418- Assertions .fail ("JsonGenerator.write(Double.NEGATIVE_INIFINITY) should produce NumberFormatException" );
419- } catch (NumberFormatException ne ) {
420- // expected
421- }
368+ Assertions .assertThrows (NumberFormatException .class , () -> generator .write (Double .NaN ),
369+ "JsonGenerator.write(Double.NaN) should produce NumberFormatException" );
370+
371+ Assertions .assertThrows (NumberFormatException .class , () -> generator .write (Double .POSITIVE_INFINITY ),
372+ "JsonGenerator.write(Double.POSITIVE_INIFINITY) should produce NumberFormatException" );
373+
374+ Assertions .assertThrows (NumberFormatException .class , () -> generator .write (Double .NEGATIVE_INFINITY ),
375+ "JsonGenerator.write(Double.NEGATIVE_INIFINITY) should produce NumberFormatException" );
376+
422377 generator .writeEnd ();
423378 generator .close ();
424379 }
@@ -428,24 +383,16 @@ void testGeneratorObjectDouble() {
428383 StringWriter writer = new StringWriter ();
429384 JsonGenerator generator = Json .createGenerator (writer );
430385 generator .writeStartObject ();
431- try {
432- generator .write ("foo" , Double .NaN );
433- Assertions .fail ("JsonGenerator.write(String, Double.NaN) should produce NumberFormatException" );
434- } catch (NumberFormatException ne ) {
435- // expected
436- }
437- try {
438- generator .write ("foo" , Double .POSITIVE_INFINITY );
439- Assertions .fail ("JsonGenerator.write(String, Double.POSITIVE_INIFINITY) should produce NumberFormatException" );
440- } catch (NumberFormatException ne ) {
441- // expected
442- }
443- try {
444- generator .write ("foo" , Double .NEGATIVE_INFINITY );
445- Assertions .fail ("JsonGenerator.write(String, Double.NEGATIVE_INIFINITY) should produce NumberFormatException" );
446- } catch (NumberFormatException ne ) {
447- // expected
448- }
386+
387+ Assertions .assertThrows (NumberFormatException .class , () -> generator .write ("foo" , Double .NaN ),
388+ "JsonGenerator.write(String, Double.NaN) should produce NumberFormatException" );
389+
390+ Assertions .assertThrows (NumberFormatException .class , () -> generator .write ("foo" , Double .POSITIVE_INFINITY ),
391+ "JsonGenerator.write(String, Double.POSITIVE_INIFINITY) should produce NumberFormatException" );
392+
393+ Assertions .assertThrows (NumberFormatException .class , () -> generator .write ("foo" , Double .NEGATIVE_INFINITY ),
394+ "JsonGenerator.write(String, Double.NEGATIVE_INIFINITY) should produce NumberFormatException" );
395+
449396 generator .writeEnd ();
450397 generator .close ();
451398 }
0 commit comments