728x90

๐Ÿ’ปAssertJ๋ž€

AssertJ๋Š” ๋งŽ์€ assertion์„ ์ œ๊ณตํ•˜๋Š” ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€์™€ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ  ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” IDE์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์— ์šฉ์ดํ•˜๋‹ค.

AssertJ๋Š” ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ๋กœ ๋งŒ๋“ค์–ด์กŒ๋‹ค. ๋งŒ์•ฝ ๋ฌด์—‡์ธ๊ฐ€ ์ž˜๋ชป๋œ๊ฒƒ์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ํ”ผ๋“œ๋ฐฑ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์›์ž‘์ž๊ฐ€ ๋งํ•˜์˜€๋‹ค.


๊ฐ€๋…์„ฑ ์ด์•ผ๊ธฐ๋ฅผ ํ•ด๋ณด์ž๋ฉด 

//JUnit
assertEquals(expected, actual); 

//AssertJ
assertThat(actual).isEqualTo(expected);

๋‘ ์˜ˆ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ ์ฒซ๋ฒˆ์งธ๊ฐ€ JUnit์—์„œ ์ œ๊ณตํ•˜๋Š” assertEquals์ด๋‹ค. ์•„๋ž˜๋Š” AssertJ์˜ AssertThat์ด๋‹ค.

AssertThat์ด ํ™•์‹คํžˆ ์ง๊ด€์ ์œผ๋กœ ๋ณด์ด๊ณ  ๊ฐ€๋…์„ฑ์ด ์˜ฌ๋ผ๊ฐ„๋‹ค.

 

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์˜ˆ์ œ

@Test 
void simple_case() { 
	assertThat("ABCD").isNotNull() 
    	    .startsWith("A")
            .contains("BC") 
            .endsWith("D"); 
         
}

๋ด๋„ ์–ด๋А์ •๋„๋Š” ์–ด๋–ค๋ง์ธ์ง€ ๊ฐ์€ ์˜จ๋‹ค. "ABCD"๋ผ๋Š” ๋ฌธ์ž์—ด์ด NULL์ด ์•„๋‹ˆ๊ณ  "A"๋กœ ์‹œ์ž‘ํ•˜๊ณ  "BC"๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๊ณ  "D"๋กœ ๋๋‚œ๋‹ค๋Š” ํ…Œ์ŠคํŠธ ์˜ˆ์ œ์ด๋‹ค.

 

์ด์ œ ๋‹ค์–‘ํ•œ ์˜ˆ์ œ๋ฅผ ๋ณด์ž


๐Ÿ’ปFiltering assertions

@Test
void filter_test1(){

    List<Human> list = new ArrayList<>();
    Human k = new Human("Kim",22);
    Human a = new Human("Aim",23);
    Human p = new Human("Park",42);
    Human j = new Human("Jin",12);
    Human y = new Human("yun",32);

    list.add(k);
    list.add(a);
    list.add(p);
    list.add(j);
    list.add(y);

    Assertions.assertThat(list).filteredOn(human ->
                human.getName().contains("i"))
                .containsOnly(a,j,k);

}

์˜ˆ๋ฅผ ๋ณด๋ฉด Humanํด๋ž˜์Šค์—๋Š”  ์ด๋ฆ„๊ณผ ๋‚˜์ด๊ฐ€ ์žˆ๋‹ค. filteredOn์€ ์ง๊ด€์ ์œผ๋กœ ๊ฑธ๋Ÿฌ๋‚ธ๋‹ค?์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ์‰ฌ์šธ๊ฒƒ์ด๋‹ค.

์ด๋ฆ„์„ ๋ชจ๋‘ ๊ฐ€์ ธ์™€์„œ ์ด๋ฆ„์ค‘์— i๊ฐ€ ํฌํ•จ๋˜๋Š” ์ด๋ฆ„์„ ๊ฑธ๋Ÿฌ๋‚ธ๋‹ค. ๊ทธ ๋‹ค์Œ containOnly๋กœ a,j,k ๊ฐ€ ๋งž๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค.

 

๐Ÿ’ป๊ฐ์ฒด์˜ property๋ฅผ ๊ฒ€์ฆ ์˜ˆ์ œ

@Test
void filter_test2() {
   
     List<Human> list = new ArrayList<>();
     Human k = new Human("Kim",22);
     Human a = new Human("Aim",23);
     Human p = new Human("Park",42);
     Human j = new Human("Jin",12);
     Human y = new Human("yun",32);

    
     list.add(k);
     list.add(a);
     list.add(p);
     list.add(j);
     list.add(y);

     Assertions.assertThat(list).
                filteredOn("age", 22)
                .containsOnly(k);
}

์œ„์™€ ์„ธํŒ…์€ ๊ฐ™๋‹ค. assertThat ๊ตฌ๋ฌธ์—์„œ ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’์„ ๊ฒ€์ฆํ•œ๋‹ค.

 

๊ฐ’์ด ํฌํ•จ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋„ ๊ฒ€์ฆ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

@Test
void filter_test3() {

        List<Human> list = new ArrayList<>();
        Human k = new Human("Kim",22);
        Human a = new Human("Aim",23);
        Human p = new Human("Park",42);
        Human j = new Human("Jin",12);
        Human y = new Human("yun",32);

        list.add(k);
        list.add(a);
        list.add(p);
        list.add(j);
        list.add(y);

        Assertions.assertThat(list)
                .filteredOn("age", notIn(22))
                .containsOnly(k);
}

notIn์•ˆ์— ๋“ค์–ด๊ฐ„ ๊ฐ’์ด ์—†๋Š” ๊ฐ์ฒด๋งŒ ๊ฒ€์ฆํ•˜๋Š”๊ฒƒ์ด๋‹ค. 

๋‚˜๋จธ์ง€๋กœ๋Š” In,not๋“ฑ์ด ์žˆ๋‹ค. not๊ณผ notIn์€ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค.

 

๐Ÿ’ปproperty ์ถ”์ถœํ•˜๊ธฐ 

extracting์„ ์‚ฌ์šฉํ•˜๋ฉด ํ…Œ์ŠคํŠธ๋ฅผ ํ• ๋•Œ  ๊ฐ์ฒด ์ด๋ฆ„์„ ๊ฒ€์ฆํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต๋ฌธ์—์„œ ์ด๋ฆ„์„ ๊บผ๋‚ด๊ณ  ๋˜ ๋‹ค๋ฅธ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ณ  ๋น„๊ตํ•˜๋Š” ๋ถˆํŽธํ•œ ๊ณผ์ •์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@Test
void filter_test4() {

      List<Human> list = new ArrayList<>();
      Human k = new Human("Kim",22);
      Human a = new Human("Aim",23);
      Human p = new Human("Park",42);
      Human j = new Human("Jin",12);
      Human y = new Human("yun",32);

      list.add(k);
      list.add(a);
      list.add(p);
      list.add(j);
      list.add(y);

      Assertions.assertThat(list).extracting("name")
                .contains("Kim","Aim","Park","Jin","yun");
}

list๋ฅผ ๋„˜๊ฒจ๋„ list์—์„œ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅด๊ณ  ๊ฑธ๋Ÿฌ์ง„ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ํ•„๋“œ๋ฅผ ์ถ”์ถœ(extracting)ํ•ด์„œ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋˜, ํ•„๋“œ๋ช…๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํด๋ž˜์Šค ํƒ€์ž…์„ ๋ช…์‹œํ•˜์—ฌ ๊ฒ€์ฆ์„ ๊ฐ•ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

@Test
void filter_test5() {

      List<Human> list = new ArrayList<>();
      Human k = new Human("Kim",22);
      Human a = new Human("Aim",23);
      Human p = new Human("Park",42);
      Human j = new Human("Jin",12);
      Human y = new Human("yun",32);

      list.add(k);
      list.add(a);
      list.add(p);
      list.add(j);
      list.add(y);

      Assertions.assertThat(list).extracting("name",String.class)
                .contains("Kim","Aim","Park","Jin","yun");
    }

 

๊ทธ๋ฆฌ๊ณ  ํŠœํ”Œ๋กœ๋„ ์ถ”์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•„๋“œ๋ฅผ ๊ฒ€์ฆํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

@Test
void filter_test6() {
     
      List<Human> list = new ArrayList<>();
      Human k = new Human("Kim",22);
      Human a = new Human("Aim",23);
      Human p = new Human("Park",42);
      Human j = new Human("Jin",12);
      Human y = new Human("yun",32);

      list.add(k);
      list.add(a);
      list.add(p);
      list.add(j);
      list.add(y);

     Assertions.assertThat(list).extracting("name","age")
                .contains(tuple("Kim",22)
                        ,tuple("Aim",23)
                        ,tuple("Park",42)
                        ,tuple("Jin",12)
                        ,tuple("yun",32));
    }

 

๐Ÿ’ปString assertions

๋ฌธ์ž์—ด ๊ฒ€์ฆ ์˜ˆ์‹œ์ด๋‹ค.

@Test
void stringAssertions(){
      String s = "ABCDE";

      Assertions.assertThat(s).startsWith("A").contains("BCD").endsWith("E");

}

 

๐Ÿ’ปDBB ์Šคํƒ€์ผ

@Test
void DBB_st()throws Exception{
        //given
        
        //when
        Throwable t = catchThrowable(()-> {
            throw new Exception("Exception");
        });
        
        //then
        Assertions.assertThat(t).isInstanceOf(Exception.class)
                .hasMessageContaining("Exception");
        
    
}

given.when.then ์ฃผ์„์œผ๋กœ ๊ฐ€๋…์„ฑ์ด ๋†’๊ณ  ์ˆœ์ฐจ์ ์œผ๋กœ ์–ด๋””์„œ ์–ด๋–ค ์ž‘์—…์„ ํ•˜๋Š”์ง€ ์•Œ๊ธฐ ์šฉ์ดํ•˜๋‹ค.

 

 

๐Ÿ’ปException test(ํ…Œ์ŠคํŠธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ)

@Test 
public void exception_assertion() { 
	assertThatThrownBy(() -> { 
    		throw new Exception("exception"); 
            	}).isInstanceOf(Exception.class) 
                .hasMessageContaining("exception"); };

assertThatThrownBy()๋Š” Trowable๋ณด๋‹ค ๊ฐ€๋…์„ฑ์ด ๋†’๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฌธ๋ฒ• ์˜ˆ์ œ

@Test 
public void exception_assertion() { 
    assertThatIOException().isThrownBy(() -> {
    	throw new IOException("exception"); 
        }) 
        .withMessage("exception") 
        .withMessageContaining("exception") 
        .withNoCause(); 
 }
@Test 
public void testException() { 
    assertThatExceptionOfType(IOException.class).isThrownBy(() -> { 
    	throw new IOException("exception"); 
       }) 
        .withMessage("exception") 
        .withMessageContaining("exception") 
        .withNoCause(); 
}

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ป์˜ˆ์ œ ์ฝ”๋“œ : https://github.com/ryudongjae/blog-ex

 

GitHub - ryudongjae/blog-ex: ๐Ÿ“๋ธ”๋กœ๊ทธ ์˜ˆ์ œ ์ฝ”๋“œ

๐Ÿ“๋ธ”๋กœ๊ทธ ์˜ˆ์ œ ์ฝ”๋“œ . Contribute to ryudongjae/blog-ex development by creating an account on GitHub.

github.com


โŒจ๏ธREFERENCE

https://pjh3749.tistory.com/241

728x90

'Dev > TestCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TestCode] JUnit5 ๊ธฐ๋ณธ ๋ฉ”๋‰ด์–ผ  (0) 2021.12.05