개발/spring
AssertJ 기본 문법 정리
나태쿤
2024. 7. 2. 21:54
728x90
spring boot 를 사용하다보면 자동으로
org.assertj.core.api 를 추가해준다.
그래서 다음과 같이 static import 해야 사용하기 편리하다
메서드명과 코드를 보면 쉽게 이해할 수 있으니 캡쳐로 충분히 이해할 수 있다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 | import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.Test; import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import static org.assertj.core.api.Assertions.*; public class AssertJ_Test { @Test public void 값이_같은지_검증() throws Exception { int number = 1; // 실제값, 기대값 assertThat(number).isEqualTo(1); } @Test public void 값이_다른지_검증() throws Exception { int number = 1; // 실제값, 기대값 assertThat(number).isNotEqualTo(10); } @Test public void null_인지_검증() throws Exception { String isNull = null; // 실제값, 기대값 assertThat(isNull).isNull(); } @Test public void null이_아닌지_검증() throws Exception { String isNotNull = "나는존재한다"; // 실제값, 기대값 assertThat(isNotNull).isNotNull(); } @Test public void 값_목록에_포함되는지_검증() throws Exception { int number = 1; List<Integer> list = List.of(1, 2, 3, 4, 5, 6); // 실제값, 기대값 assertThat(number).isIn(list); } @Test public void 값_목록에_포함_되지_않는지_검증() throws Exception { int number = 10; List<Integer> list = List.of(1, 2, 3, 4, 5, 6); // 실제값, 기대값 assertThat(number).isNotIn(list); } @Test public void 값보다_작은지_검증() throws Exception { int number = 10; // 실제값, 기대값 assertThat(number).isLessThan(100); } @Test public void 값보다_작은지_검증2() throws Exception { int nowYear = LocalDate.now().getYear(); int afterYear = LocalDate.now().plusYears(5L).getYear(); // 실제값, 기대값 assertThat(nowYear).isLessThan(afterYear); } @Test public void 값보다_작은지_검증3() throws Exception { LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = LocalDateTime.now().plusDays(15).atZone(ZoneId.systemDefault()); Date nowDate = new Date(); Date afterDate = Date.from(zonedDateTime.toInstant()); // 실제값, 기대값 assertThat(nowDate.getTime()).isLessThan(afterDate.getTime()); } @Test public void 값보다_작거나_같은지_검증() throws Exception { // 실제값, 기대값 // => 10 은 20보다 작거나 같다 assertThat(10).isLessThanOrEqualTo(20); } @Test public void 값보다_큰지_검증() throws Exception { // 실제값, 기대값 // => 20 은 10 보다 크다 assertThat(20).isGreaterThan(10); } @Test public void 값보다_크거나_같은지_검증() throws Exception { // 실제값, 기대값 // => 20 은 20 보다 크거나 같다 assertThat(20).isGreaterThanOrEqualTo(20); } @Test public void 값_A와_값_B_사이에_포함된다() throws Exception { // 실제값, 기대값 // => 값 50은 1과 100사이에 포함된다 assertThat(50).isBetween(1, 100); } @Test public void True_인지_검증() throws Exception { LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = LocalDateTime.now().plusDays(15).atZone(ZoneId.systemDefault()); Date nowDate = new Date(); Date afterDate = Date.from(zonedDateTime.toInstant()); // 실제값, 기대값 // nowDate 가 afterDate 보다 과거이냐? assertThat(nowDate.before(afterDate)).isTrue(); } @Test public void False_인지_검증() throws Exception { LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = LocalDateTime.now().plusDays(15).atZone(ZoneId.systemDefault()); Date nowDate = new Date(); Date afterDate = Date.from(zonedDateTime.toInstant()); // 실제값, 기대값 // afterDate 가 nowDate 보다 과거이냐? assertThat(afterDate.before(nowDate)).isFalse(); } /** * String */ @Test public void 문자열을_다_포함_하는지_검증() throws Exception { // 실제값, 기대값 // "피자나라치킨공주" 글자에는 "피자"와 "치킨"이라는 글자가 포함된다 assertThat("피자나라치킨공주").contains("피자", "치킨"); } @Test public void 해당_문자열이_딱_한번만_존재_하는지_검증() throws Exception { // 실제값, 기대값 // "피자나라치킨공주" 글자에는 "피자" 라는 글자가 딱 한번만 포함된다 assertThat("피자나라치킨공주").containsOnlyOnce("피자"); } @Test public void 문자열에_오직_숫자만_있는지_검증() throws Exception { String phone = "01012345678"; // 실제값, 기대값 // phone 이라는 문자열 변수에 오직 숫자로 이루어져 있다 assertThat(phone).containsOnlyDigits(); } @Test public void 문자열에_공백이_있는지_검증() throws Exception { String text = "white space"; // 실제값, 기대값 // text 이라는 문자열 변수에 공백 문자가 포함된다 assertThat(text).containsWhitespaces(); } @Test public void 공백_으로만_이루어진_문자열_인지_검증() throws Exception { String whiteSpaceText = " "; // 실제값, 기대값 // whiteSpaceText 이라는 문자열 변수는 공백문자로만 이루어져 있다 assertThat(whiteSpaceText).containsOnlyWhitespaces(); } @Test public void 정규_표현식을_만족하는지_검증() throws Exception { String phone = "01012345678"; // 실제값, 기대값 // phone 이라는 문자열 변수는 숫자로만 이루어져 있다 assertThat(phone).containsPattern("^[0-9]+$"); } @Test public void 문자열이_글자를_포함하지_않는지_검증() throws Exception { String foodNames = "치킨과피자"; // 실제값, 기대값 // foodNames 이라는 문자열 변수는 "햄버거", "스파게티" 라는 글자가 없다 assertThat(foodNames).doesNotContain("햄버거", "스파게티"); } @Test public void 공백_문자_를_포함하지_않은지_검증() throws Exception { String foodNames = "치킨과피자"; // 실제값, 기대값 // foodNames 이라는 문자열 변수는 공백문자가 없다 assertThat(foodNames).doesNotContainAnyWhitespaces(); } @Test public void 공백_문자_로만_이루어진_문자가_아닌지_검증() throws Exception { String message = "Hello "; // 실제값, 기대값 // message 이라는 문자열 변수는 공백문자로만 이루어져 있지 않다 (즉, 글자가 있다) assertThat(message).doesNotContainOnlyWhitespaces(); } @Test public void 정규_표현식을_만족하지_않는지_검증() throws Exception { String phone = "홍길동01012345678"; // 실제값, 기대값 // phone 이라는 문자열 변수는 숫자로만 이루어져 있지 않다 assertThat(phone).doesNotContainPattern("^[0-9]+$"); } @Test public void 특정_문자로_시작_하는지_검증() throws Exception { String frameWork = "spring data jpa"; // 실제값, 기대값 // frameWork 이라는 문자열 변수는 "spring" 이라는 글자로 시작한다 assertThat(frameWork).startsWith("spring"); } @Test public void 특정_문자로_시작_하지_않는지_검증() throws Exception { String application = "egov spring application"; // 실제값, 기대값 // application 이라는 문자열 변수는 "spring" 이라는 글자로 시작한다 assertThat(application).doesNotStartWith("spring"); } @Test public void 특정_문자로_끝나는지_하는지_검증() throws Exception { String frameWork = "spring data jpa"; // 실제값, 기대값 // frameWork 이라는 문자열 변수는 "jpa" 이라는 글자로 끝난다 assertThat(frameWork).endsWith("jpa"); } @Test public void 특정_문자로_끝나지_않는지_검증() throws Exception { String application = "egov spring application"; // 실제값, 기대값 // application 이라는 문자열 변수는 "egov" 이라는 글자로 끝나지 않는다 assertThat(application).doesNotEndWith("egov"); } /** * 숫자에 대한 추가 검증 */ @Test public void 값이_0_인지_검증() throws Exception { int zeroCount = 0; // 실제값, 기대값 // zeroCount 은 0 이다 assertThat(zeroCount).isZero(); } @Test public void 값이_0_이_아닌지_검증() throws Exception { int count = 10; // 실제값, 기대값 // count 은 0 이 아니다 assertThat(count).isNotZero(); } @Test public void 값이_0_이_양수_인지_검증() throws Exception { int age = 132; // 실제값, 기대값 // age 은 양수이다 assertThat(age).isPositive(); assertThat(age).isNotNegative(); } @Test public void 값이_0_이_음수_인지_검증() throws Exception { int myCash = -1; // 실제값, 기대값 // myCash 은 음수이다 assertThat(myCash).isNotPositive(); assertThat(myCash).isNegative(); } /** * 날짜에 대한 검증 */ @Test public void 비교할_값_보다_이전_날짜_인지_검증() throws Exception { LocalDateTime nowDateTime = LocalDateTime.now(); LocalDateTime afterDateTime = LocalDateTime.of(2030, 12, 31, 23, 59, 59); // 실제값, 기대값 // nowDateTime 은 afterDateTime 보다 이전이다 assertThat(nowDateTime).isBefore(afterDateTime); } @Test public void 비교할_값_보다_이전_이거나_같은_날짜_인지_검증() throws Exception { LocalDateTime nowDateTime = LocalDateTime.now(); LocalDateTime afterDateTime = LocalDateTime.of( nowDateTime.getYear(), nowDateTime.getMonthValue(), nowDateTime.getDayOfMonth(), nowDateTime.getHour(), nowDateTime.getMinute(), nowDateTime.getSecond(), nowDateTime.getNano() ); LocalDateTime beforeDateTime = LocalDateTime.now().minusDays(1); // 실제값, 기대값 // nowDateTime 은 afterDateTime 보다 이전 이거나 같다 assertThat(nowDateTime).isBeforeOrEqualTo(afterDateTime); // beforeDateTime 은 nowDateTime 보다 이전 이거나 같다 assertThat(beforeDateTime).isBeforeOrEqualTo(nowDateTime); } @Test public void 비교할_값_보다_이후_날짜_인지_검증() throws Exception { LocalDateTime nowDateTime = LocalDateTime.now(); LocalDateTime afterDateTime = LocalDateTime.now().plusDays(10); // 실제값, 기대값 // afterDateTime 은 nowDateTime 보다 이후 이다(미래) assertThat(afterDateTime).isAfter(nowDateTime); } @Test public void 비교할_값_보다_이후_이거나_같은_날짜_인지_검증() throws Exception { LocalDateTime nowDateTime = LocalDateTime.now(); LocalDateTime afterDateTime = LocalDateTime.now().plusYears(1); // 실제값, 기대값 // afterDateTime 은 nowDateTime 보다 이후 이거나 같다 assertThat(afterDateTime).isAfterOrEqualTo(nowDateTime); } @Test public void 날짜_의_특정_칼럼_을_제외하고_나머지_값이_같은지_검증() throws Exception { LocalDateTime nowDateTime = LocalDateTime.now(); LocalDateTime beforeDateTime1 = LocalDateTime.now().minusNanos(1); LocalDateTime beforeDateTime2 = LocalDateTime.now().minusSeconds(1); LocalDateTime beforeDateTime3 = LocalDateTime.now().minusMinutes(1); LocalDateTime beforeDateTime4 = LocalDateTime.now().minusHours(1); // 실제값, 기대값 // nowDateTime 은 beforeDateTime1 의 나노초를 제외한 시간대는 같다 assertThat(nowDateTime).isEqualToIgnoringNanos(beforeDateTime1); // nowDateTime 은 beforeDateTime2 의 초 이하를 제외한 시간대는 같다 assertThat(nowDateTime).isEqualToIgnoringSeconds(beforeDateTime2); // nowDateTime 은 beforeDateTime3 의 분 이하를 제외한 시간대는 같다 assertThat(nowDateTime).isEqualToIgnoringMinutes(beforeDateTime3); // nowDateTime 은 beforeDateTime3 의 시간 이하를 제외한 시간대는 같다 assertThat(nowDateTime).isEqualToIgnoringHours(beforeDateTime4); } /** * 컬렉션에 대한 검증 */ // List Or Set @Test public void 컬렉션의_크기가_기대한_값과_같은지_검증() throws Exception { List<Integer> list = List.of(1, 2, 3, 4, 5); // 실제값, 기대값 // list 은 크기가 5 이다 assertThat(list).hasSize(5); } @Test public void 컬렉션이_해당_값을_포함_하는지_검증() throws Exception { List<Integer> list = List.of(1, 2, 3, 4, 5); // 실제값, 기대값 // list 은 1,2,3 을 포함한다 assertThat(list).contains(1, 2, 3); } @Test public void 컬렉션이_지정한_값만_포함_하는지_검증() throws Exception { List<Integer> list = List.of(1, 2, 3, 4); // 실제값, 기대값 // list 은 정확히 1,2,3,4 만 가지고 있다 assertThat(list).containsOnly(1, 2, 3, 4); } @Test public void 컬렉션이_지정한_값의_일부를_포함_하는지_검증() throws Exception { List<Integer> list = List.of(1, 2, 3, 4); // 실제값, 기대값 // list 은 1, 3, 5, 7, 9 중에 일부를 포함한다 (1, 3) assertThat(list).containsAnyOf(1, 3, 5, 7, 9); } @Test public void 컬렉션이_지정한값을_딱_한번만_포함_하는지_검증() throws Exception { List<Integer> list = List.of(1, 2, 3, 4); // 실제값, 기대값 // list 은 1, 3 을 딱 한번만 포함한다 assertThat(list).containsOnlyOnce(1,3); } // Map @Test public void Map이_지정한_키를_포함_하는지_검증() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); // 실제값, 기대값 // map 은 age 라는 키를 가지고 있다 assertThat(map).containsKey("age"); } @Test public void Map이_지정한_키들을_포함_하는지_검증() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); // 실제값, 기대값 // map 은 age, name 라는 키들을 가지고 있다 assertThat(map).containsKeys("age", "name"); } @Test public void Map이_지정한_키만_포함_하는지_검증() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); // map.put("name2", "ksj"); // 실제값, 기대값 // map 은 age, name 라는 키들만 가지고 있다 assertThat(map).containsOnlyKeys("age", "name"); } @Test public void Map이_지정한_키를_포함_하지_않는지_검증() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); // 실제값, 기대값 // map 은 title 라는 키를 갖지 않는다 assertThat(map).doesNotContainKey("title"); } @Test public void Map이_지정한_키들을_포함_하지_않는지_검증() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); // 실제값, 기대값 // map 은 title 라는 키를 갖지 않는다 assertThat(map).doesNotContainKeys("title", "content"); } @Test public void Map이_지정한_엔트리_포함_하는지_검증() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); Map.Entry<String, Integer> entry = Map.entry("age", 132); // 실제값, 기대값 // map 은 Map.entry("age", 132) 라는 엔트리를 갖는지 검증 assertThat(map).contains(entry); } @Test public void Map이_지정한_엔트리_포함_하는지_검증2() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("age", 132); map.put("name", "ksj"); // 실제값, 기대값 // map 은 키는 "age" 이고 값은 132 인 엔트리를 갖는지 검증 assertThat(map).containsEntry("age", 132); } /** * 익셉션 관련 검증 */ @Test public void 익셉션이_발생_하는지_검증() throws Exception { assertThatThrownBy(() -> { throw new IllegalArgumentException(); }); } @Test public void 해당_타입의_익셉션이_발생_하는지_검증() throws Exception { assertThatThrownBy(() -> { throw new IllegalArgumentException(); }).isInstanceOf(RuntimeException.class); } @Test public void 해당_타입의_익셉션이_발생_하는지_검증_두번째_방법() throws Exception { assertThatExceptionOfType(RuntimeException.class) .isThrownBy(() -> { throw new IllegalArgumentException(); }); } @Test public void IOException_익셉션이_발생_하는지_검증() throws Exception { assertThatIOException().isThrownBy(() -> { throw new IOException(); }); } @Test public void 익셉션이_발생_하지_않는지_검증() throws Exception { assertThatCode(() -> { // code... System.out.println("예외가 발생하지 않습니다"); }).doesNotThrowAnyException(); } @Test public void 여러_검증을_모아서_검증하기() throws Exception { // 테스트가 실패해야 정상, 실패했을때 나오는 콘솔을 확인 SoftAssertions soft = new SoftAssertions(); soft.assertThat(1).isEqualTo(1); soft.assertThat(10).isGreaterThan(20); // <- 거짓 soft.assertThat(10).isGreaterThan(30); // <- 거짓 soft.assertThat("1234567890").containsOnlyDigits(); soft.assertAll(); } @Test public void 여러_검증을_모아서_검증하기_두번째_방법() throws Exception { // 테스트가 실패해야 정상, 실패했을때 나오는 콘솔을 확인 SoftAssertions.assertSoftly((soft) -> { soft.assertThat(1).isEqualTo(1); soft.assertThat(10).isGreaterThan(20); // <- 거짓 soft.assertThat(10).isGreaterThan(30); // <- 거짓 soft.assertThat("1234567890").containsOnlyDigits(); }); } /** * 테스트 설명달기 */ @Test public void 테스트에_설명_달기_1() throws Exception { // 실패해야 정상 assertThat(1) .as("1은 양의 정수이다") .isNegative(); } @Test public void 테스트에_설명_달기_2() throws Exception { String message = "1은 양의 정수이다"; // 실패해야 정상 assertThat(1) .as("[정수 검사] %s", message) .isNegative(); } @Test public void 테스트에_설명_달기_3() throws Exception { // 실패해야 정상 assertThat(1) .describedAs("1은 양의 정수이다") .isNegative(); } @Test public void 테스트에_설명_달기_4() throws Exception { // 실패해야 정상 String message = "1은 양의 정수이다"; // 실패해야 정상 assertThat(1) .describedAs("[정수 검사] %s", message) .isNegative(); } } | cs |
728x90