AssertJ๋ ํ
์คํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๋ฉ์๋ ์ฒด์ด๋์ ์ง์ํ๊ธฐ ๋๋ฌธ์ ์ข ๋ ๊น๋ํ๊ณ ์ฝ๊ธฐ ์ฌ์ด ํ
์คํธ ์ฝ๋ ์์ฑ ๊ฐ๋ฅ(๊ฐ๋
์ฑ์ด ์ข์)
๊ฐ๋ฐ์๊ฐ ํ
์คํธ๋ฅผ ํ๋ฉด์ ํ์ํ๋ค๊ณ ์์ํ ์ ์๋ ๊ฑฐ์ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์ ๊ณต
์์กด์ฑ
spring-boot-starter-test
์ ๊ธฐ๋ณธ์ ์ผ๋ก ํฌํจ๋์ด์๋ค. spring-boot-starter-test
๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ด ์ถ๊ฐํ ์ ์๋ค.
Gradle
Copy testCompile 'org.assertj:assertj-core:3.21.0'
Maven
Copy <dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.21.0</version>
<scope>test</scope>
</dependency>
ํ
์คํธ ์ฝ๋ ์์ฑ ์์ํ๊ธฐ
Assertions.assertThat()
๋ชจ๋ ํ
์คํธ ์ฝ๋๋ assertThat()
๋ฉ์๋์์ ์์ํ๋ค.
Copy assertThat(ํ
์คํธ ํ๊ฒ)
.method1()
.method2()
.method3();
ํ
์คํธ ์คํจ ๋ฉ์ธ์ง
ํ
์คํธ ์คํจ์ ๋ํ๋ผ ๋ฉ์ธ์ง๋ฅผ .as()
๋ก ํํํ ์ ์๋ค.
๋ฐ๋์ ๊ฒ์ฆ ๋ฌธ ์์ ์์ฑ๋์ด์ผํ๋ค.
Copy @Test
void asExample() {
String blank = " ";
assertThat(StringUtils.hasText(blank))
.as("check '%s' is null", blank)
.isTrue();
}
๋ค์๊ณผ ๊ฐ์ด ์์ฑํด๋๋ฉด ํ
์คํธ ์คํจ์ ์ฝ์์ฐฝ์ ์๋ฌ๋ฉ์ธ์ง๊ฐ ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
Copy [check ' ' is null]
Expecting value to be true but was false
org.opentest4j.AssertionFailedError: [check ' ' is null]
๋ค์๊ณผ ๊ฐ์ด ๊ฒ์ฆ๋ฌธ ๋ค์ ์์ฑํ ๊ฒฝ์ฐ ํธ์ถ๋์ง ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
Copy @Test
void asExample() {
String blank = " ";
assertThat(StringUtils.hasText(blank))
.isTrue()
.as("check '%s' is null", blank);
}
Copy Expecting value to be true but was false
org.opentest4j.AssertionFailedError:
Expecting value to be true but was false
Collection ํํฐ๋ง
.filteredOn()
.filteredOn()
์ผ๋ก ํ
์คํธํ ๋ฐ์ดํฐ๋ค์ ํํฐ๋ง ํ ์ ์๋ค.
Copy @Test
void test() {
EnumSet<DayOfWeek> days = EnumSet.allOf(DayOfWeek.class);
assertThat(days)
.filteredOn(d -> d.getValue() > 4)
.containsOnly(DayOfWeek.FRIDAY, DayOfWeek.SATURDAY, DayOfWeek.SUNDAY);
}
์ด๋ not
, in
, notIn
์ ๋ํ ๊ฐ์ฒด ํ๋กํผํฐ๋ฅผ ๊ฒ์ฆํ ์ ๋ ์๋ค.
Copy List<Human> list = new ArrayList<>();
list.add(new Human("faker", 26));
list.add(new Human("bang", 26));
list.add(new Human("wolf", 26));
list.add(new Human("keria", 20));
assertThat(list)
.filteredOn("age", not(26))
.containsOnly(list.get(3));
๊ธฐ์กด์ ํด๋์ค์ ํน์ ๋ฐ์ดํฐ๋ฅผ ๋น๊ตํ๊ธฐ ์ํด์๋ ๋ณ๋ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด ๋ค์๊ณผ ๊ฐ์ด ํ
์คํธ๋ฅผ ์งํํด์ผํ๋ค.
Copy @Test
void no_extracting() throws Exception{
List<String> names = new ArrayList<>();
for (T1 member : members) {
names.add(member.getName());
}
assertThat(names).containsOnly("faker", "bang", "wolf", "keria");
}
ํ์ง๋ง assertj์์ extracting
์ ์ง์ํด์ฃผ๋ฉด์ ํ์ํ ๋ฐ์ดํฐ๋ง ์ถ์ถํด ํ
์คํธ๊ฐ ๊ฐ๋ฅํด์ก๋ค.
Copy @Test
void extracting() {
List<T1> members = new ArrayList<>();
members.add(new T1("faker", 26));
members.add(new T1("bang", 26));
members.add(new T1("wolf", 26));
members.add(new T1("keria", 20));
assertThat(members)
.extracting("name")
.contains("faker", "bang", "wolf", "keria");
assertThat(members)
.extracting("name", "age")
.contains(tuple("faker", 26),
tuple("bang", 26),
tuple("wolf", 26),
tuple("keria", 20));
}
์ถ์ถํ ๋ฐ์ดํฐ๊ฐ ํ๊ฐ๋ผ๋ฉด ํ์
์ง์ ์ด ๊ฐ๋ฅํ๋ฉฐ, ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ์๋ assertj์์ ์ง์ํด์ฃผ๋ tuple()
๋ก ํ
์คํธํ ์ ์๋ค.
SoftAssertions
Assertions.assertThat()
์ ํ
์คํธ๊ฐ ํ๊ฐ๋ผ๋ ์คํจํ๋ฉด ํด๋น ํ
์คํธ๋ ์ค์ง๋๋ค.
SoftAssertions
์ ์ด์ฉํ๋ฉด ๋ชจ๋ assertThat์ ์คํํ๊ณ ํด๋น ์คํจ ๋ด์ญ์ ํ์ธํ ์ ์๋ค.
Copy @Test
void test() {
String str = "test";
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(StringUtils.hasText(str))
.as("check '%s' is not null", str)
.isFalse();
softAssertions.assertThat(StringUtils.hasLength(str))
.as("check '%s' is not null", str)
.isTrue();
});
}
Copy Multiple Failures (1 failure)
-- failure 1 --
[check 'test' is not null]
Expecting value to be false but was true
at SoftAssertionTest.lambda$test$0(SoftAssertionTest.java:15)
org.assertj.core.error.AssertJMultipleFailuresError:
Multiple Failures (1 failure)
-- failure 1 --
์ฐธ๊ณ