๐ป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
'Dev > TestCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TestCode] JUnit5 ๊ธฐ๋ณธ ๋ฉ๋ด์ผ (0) | 2021.12.05 |
---|