๐งช ๋จ์ ํ ์คํธ(Unit Test)๋ ?
JUnit์ Java ์์ ๋
๋ฆฝ๋ ๋จ์ํ
์คํธ๋ฅผ ์ง์ํด์ฃผ๋ ํ๋ ์์ํฌ์ด๊ธฐ ๋๋ฌธ์
๋จ์ ํ
์คํธ์ ๋ํด ๋จผ์ ์์๋ณด๊ฒ ์ต๋๋ค ๐ก
ํ ์คํธ ๋์ ๋จ์๊ฐ ์๊ฒฉํ๊ฒ ์ ํด์ ธ์์ง๋ ์์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ํด๋์ค ๋๋ ๋ฉ์๋ ์์ค์ผ๋ก ์ ํด์ง๋๋ค. ๋จ์์ ํฌ๊ธฐ๊ฐ ์์์๋ก ๋จ์์ ๋ณต์ก์ฑ์ด ๋ฎ์์ง๊ณ , ์คํํ๋ ค๋ ๊ธฐ๋ฅ์ ํํํ๊ธฐ ๋ ์ฌ์์ง๋๊ฒ ๋น์ฐํ๊ฒ ์ฃ ?
๋จ์ํ
์คํธ๋ ํน์ ์์ค์ฝ๋์ ๋ชจ๋์ด ์๋ํ ๋๋ก ์ ์๋ํ๋์ง ๊ฒ์ฆํ๋ ํ
์คํธ์
๋๋ค.
์ฆ, ํจ์ ๋ฐ ๋ฉ์๋์ ๋ํ ํ
์คํธ๋ฅผ ํ๋ ์์
์ผ๋ก์จ, Spring์์ ๋จ์ํ
์คํธ๋ Spring Container์ ์ฌ๋ผ์ ์๋ Bean ์ ํ
์คํธ ํ๋ ๊ฒ์
๋๋ค.
๐ก ๋จ์ ํ ์คํธ์ ์ฅ์
๋จ์ ํ
์คํธ๋ ํด๋น ๋ถ๋ถ๋ง ๋
๋ฆฝ์ ์ผ๋ก ํ
์คํธํ๊ธฐ ๋๋ฌธ์ ์ด๋ค ์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋งํ์ฌ๋ ๋น ๋ฅด๊ฒ ๋ฌธ์ ์ฌ๋ถ๋ฅผ ํ์
ํ ์ ์์ต๋๋ค.
๋ํ, ๊ฐ๋ฐ์๋ ์์ฑํ ํ
์คํธ ์ฝ๋๋ฅผ ์์๋ก ๋น ๋ฅด๊ฒ ๋๋ฆฌ๋ฉด์ ๋ฌธ์ ์ ์ ํ์
ํ ์ ์์ต๋๋ค.
๋จ์ ํ
์คํธ๋ฅผ ์งํํ๋ ๋์์๋ ์ค์ ์ดํ๋ฆฌ์ผ์ด์
์ ๋์์ํฌ ๋์ ๋ง์ฐฌ๊ฐ์ง๋ก Bean์ ํ
์คํธ ํ ์ ์์ต๋๋ค ๐ง๐ผ๐ป
๐งช JUnit ์ด๋?
- ์๋ฐ ๊ฐ๋ฐ์๊ฐ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ํ ์คํ ๊ธฐ๋ฐ ํ๋ ์์ํฌ ์ ๋๋ค.
- ํ์ฌ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๊ณ ์๋ ๋ฒ์ ์ JUnit 5 ์ ๋๋ค.
- ๊ธฐ๋ณธ Junit 4์ ๋นํด JUnit 5๋ 3๊ฐ์ง ๋ชจ๋๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
- JUnit5๋ ํ ์คํธ ์์ฑ์๋ฅผ ์ํ API ๋ชจ๋๊ณผ ํ ์คํธ ์คํ์ ์ํ API๊ฐ ๋ถ๋ฆฌ๋์ด ์์ต๋๋ค.
- ์๋ฐ 8 ์ด์์ ํ์๋ก ํฉ๋๋ค.
๐งช JUnit ๊ตฌ์กฐ

ํด๋น ๊ตฌ์กฐ์ ๋ํด ๊ฐ๋จํ ์์๋ณด๊ฒ ์ต๋๋ค ๐ก
๊ธฐ์กด ๋ฒ์ ์ธ JUnit4์์๋ vintage-engine ๋ชจ๋์ ์ฌ์ฉํด์ ํ
์คํ
์ ์งํํ์ต๋๋ค.
๊ทธ๋ฌ๋ ์ด์ ๋ฌ๋ฆฌ, JUnit5๋ ํฌ๊ฒ 3๊ฐ์ง ๋ชจ๋๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
JUnit5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
JUnit Jupiter API : JUnit5์ ์๋ก ์ถ๊ฐ๋ ํ ์คํธ ์ฝ๋์ฉ API
- JUnit Platform : ํ
์คํธ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ํ
์คํธ ๊ณํ์ ์์ฑํ๋ TestEngine ์ธํฐํ์ด์ค๋ฅผ ์ ์ ํฉ๋๋ค.
์ฆ, ๊ธฐ์ด์ ์ธ ์ญํ ์ ์ํํ๋ฉฐ TestEngine API ๋ฅผ ์ ๊ณตํฉ๋๋ค. - JUnit Jupiter : JUnit5์์ ํ
์คํธ ๋ฐ Extension์ ์์ฑํ๊ธฐ ์ํ ์๋ก์ด ํ๋ก๊ทธ๋๋ฐ ๋ชจ๋ธ๊ณผ ํ์ฅ ๋ชจ๋ธ์ ์กฐํฉ์ด๋ฉฐ, TestEngine API ๊ตฌํ์ฒด ์
๋๋ค.
JUpiter ๋ ํ ์คํธ ์ฝ๋ ์์ฑ์ ํ์ํ junit-jupiter-api ๋ชจ๋๊ณผ ํ ์คํธ ์คํ์ ์ํ junit-jupiter-engine ๋ชจ๋๋ก ๋ถ๋ฆฌ๋์ด ์๋ค.- JUnit Jupiter = junit-jupiter-api + junit-jupiter-engine
- JUnit Vintage : JUnit3, JUnit4 ๋ฅผ ์คํํ ์ ์๋ TestEngine ์
๋๋ค. ํ์ ํธํ์ฑ์ ์ํด์ ์กด์ฌํฉ๋๋ค.
TestEngine์ ์ค์ ๊ตฌํ์ฒด๋ ๋ณ๋ ๋ชจ๋๋ก ์กด์ฌํ๋๋ฐ, ์ด ์ค ํ๋๊ฐ jupiter-engine ์ ๋๋ค.
๐งช ํ ์คํธ์ Class & Method ์ด๋ ธํ ์ด์
Test ์ญ์ ์๋ฐ์ ๊ฐ์ฒด๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
๋ฐ๋ผ์ ํด๋์ค๋ก ๊ตฌํ์ ํ๊ธฐ ๋๋ฌธ์ Class + Method ํํ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
- Class : ์ต์์ ํด๋์ค, static member class, @Nested class ์ค ์ ์ด๋ ํ๋์ ๋ฉ์๋๋ฅผ ํฌํจํ๋ ํด๋์ค
- Test Class ๋ absctract ์ด๋ฉด ์๋๋ฉฐ, ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํฌํจํด์ผ ํฉ๋๋ค ๐ช
- Test Method : @Test, @RepeatedTest, @TestFactory ๋ฑ์ ์ด๋
ธํ
์ด์
์ด ์ ์ธ๋์ด ์๋ ๋ฉ์๋๋ก ์ค์ ํ
์คํธํ๊ณ ์ ํ๋ ๋ด์ฉ์ ๋ฃ์ต๋๋ค.
- @Test
: ํด๋น ๋ฉ์๋๊ฐ ํ ์คํธ ๋ฉ์๋์์ ๋ํ๋ ๋๋ค. - @ParametherizedTest
: ํด๋น ๋ฉ์๋๊ฐ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ํ ์คํธ์์ ๋ํ๋ ๋๋ค. - @RepeatedTest
: ๋ฐ๋ณต๋๋ ํ ์คํธ ๋ฉ์๋๋ฅผ ๋ํ๋ ๋๋ค. -> @RepeatedTest(3)
- @Test
- LifeCycle Method : @BeforeAll, @BeforeEach, @AfterAll, @AfterEach ๊ฐ ์ ์ธ๋์ด ์๋ ๋ฉ์๋๋ก Test ์ Life Cycle์ ๋ฐ๋ผ ์คํ๋๋ ๋ฉ์๋ ์
๋๋ค.
- @BeforeAll
: ํ ์คํธ Class ๊ธฐ์ค์ผ๋ก ํ ์คํธ ๋ฉ์๋๋ค์ด ์คํ๋๊ธฐ ์ ์คํ
์ฆ, ํ ์คํธ ์์ ์์ ์ ๋ฑ ํ๋ฒ ์คํ(static ๋ฉ์๋๋ง ๊ฐ๋ฅ) - @BeforeEach
: ๊ฐ ํ ์คํธ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์คํ
์ผ๋ฐ์ ์ผ๋ก Mock ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ฆฌ ์ธํ ํ๊ธฐ ์ํด ์ฌ์ฉ - @AfterAll
: ํ ์คํธ Class ๊ธฐ์ค์ ํ ์คํธ ๋ฉ์๋๋ค์ด ์คํ๋ ํ ์คํ
์ฆ, ํ ์คํธ ์ข ๋ฃ ์์ ์ ๋ฑ ํ๋ฒ ์คํ(static ๋ฉ์๋๋ง ๊ฐ๋ฅ) - @AfterEach
: ๊ฐ ํ ์คํธ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ์คํ
- @BeforeAll
๐ ์ ์ฒด์ ์ธ Life Cycle
BeforeAll -> BeforeEach -> Test -> AfterEach -> AfterAll
๐งช ๊ธฐํ ์ด๋ ธํ ์ด์
- @Tag : ํ ์คํธ ํด๋์ค, ๋ฉ์๋์ ํ ์คํธ ๊ตฌ๋ถ์ ํํนํ๊ธฐ ์ํด ์ฌ์ฉ
- @Disabled : ํด๋น ํด๋์ค๋ ํ ์คํธ๋ฅผ ์ฌ์ฉํ์ง ์์
- @Timeout : ํ ์คํธ ์คํ ์๊ฐ ์ ์ธ, ์ด๊ณผ๋๋ฉด ์คํจํ๋๋ก ์ค์
- @ExtendWith : ํ์ฅ์ ์ ์ธ์ ์ผ๋ก ๋ฑ๋กํ ๋ ์ฌ์ฉ
- @TempDir : ํ๋ ์ฃผ์ ํน์ ๋งค๊ฐ๋ณ์ ์ฃผ์ ์ ํตํด ์์ ๋๋ ํ ๋ฆฌ ์ ๊ณต
- @DispayName : ํ ์คํธ ๋ฉ์๋ ์ด๋ฆ ์ค์ (๊ธฐ๋ณธ๊ฐ -> ๋ฉ์๋ ์ด๋ฆ)
๐งช Assertions
org.junit.jupiter.api.Assertions ํจํค์ง๋ฅผ ํตํด ์ฌ์ฉ ๊ฐ๋ฅ
assertArrayEquals
์์ ๋ฐฐ์ด(expected)๊ณผ ์ค์ ๋ฐฐ์ด(actual))์ด ๋์ผํ์ง ํ์ธํฉ๋๋ค.
๋ ๋ฐฐ์ด์ด ์ผ์น ํ์ง ์๋ค๋ฉด, ๋ง์ง๋ง ์ธ์์ธ ๋ฉ์ธ์ง๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
|
|
|
|
|
public void whenAssertingArraysEquality_thenEqual() { |
|
|
char[] expected = { 'J', 'u', 'p', 'i', 't'}; |
|
|
char[] actual = "Jupiter".toCharArray(); |
|
|
|
|
|
assertArrayEquals(expected, actual, "๋ ๋ฐฐ์ด์ด ์ผ์นํ์ง ์์ต๋๋ค."); |
|
|
} |
assertEquals
๋ ๊ฐ์ ๋น๊ตํ์ฌ ์ผ์นํ๋์ง ํ์ธํฉ๋๋ค.
|
|
|
|
|
public void whenAssertingEquality_thenEqual() { |
|
|
float square = 2 * 2; |
|
|
float rectangle = 2 * 2; |
|
|
|
|
|
assertEquals(square, rectangle); |
|
|
} |
์ค์ ๊ฐ์ด ์์ ๊ฐ์ ์ฐจ์ด๊ฐ ์์ ๊ฒฝ์ฐ, ์ธ ๋ฒ์งธ ์ธ์๋ก ์ฐจ์ด๊ฐ ๋๋ ๊ฐ์ ๊ฐ์ง delta๋ฅผ ์ถ๊ฐํฉ๋๋ค
|
|
|
|
|
void whenAssertingEqualityWithDelta_thenEqual() { |
|
|
float square = 2 * 2; |
|
|
float rectangle = 3 * 2; |
|
|
float delta = 2; |
|
|
|
|
|
assertEquals(square, rectangle, delta); |
|
|
} |
assertNotEquals
์์ ๊ฐ๊ณผ ์ค์ ๊ฐ์ด ๋ค๋ฅธ์ง ํ์ธ ํฉ๋๋ค.
๊ฐ์ ๊ฒฝ์ฐ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
|
|
|
|
|
void whenAssertingEquality_thenNotEqual() { |
|
|
Integer value = 5; |
|
|
|
|
|
assertNotEquals(0, value, "The result cannot be 0"); |
|
|
} |
assertTrue and assertFalse
assertTrue๋ ์ฃผ์ด์ง ์กฐ๊ฑด์ด ์ฐธ์ธ์ง ํ์ธํฉ๋๋ค.
์กฐ๊ฑด์ด ๊ฑฐ์ง์ผ ๊ฒฝ์ฐ ๋ง์ง๋ง ์ธ์์ธ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
|
|
|
|
|
void whenAssertingConditions_thenVerified() { |
|
|
assertTrue(5 > 4, "5 is greater the 4"); |
|
|
assertTrue(null == null, "null is equal to null"); |
|
|
} |
assertFalse๋ ์ฃผ์ด์ง ์กฐ๊ฑด์ด ๊ฑฐ์ง์ธ์ง ํ์ธ ํฉ๋๋ค.
์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์ฐ ๋ง์ง๋ง ์ธ์์ธ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
|
|
|
|
|
public void givenBooleanSupplier_whenAssertingCondition_thenVerified() { |
|
|
assertFalse(5 > 6, "5 is not greater then 6"); |
|
|
} |
assertNull and assertNotNull
assertNull์ ๊ฐ์ฒด์ null ์ฌ๋ถ๋ฅผ ํ์ธ ํฉ๋๋ค.
๊ฐ์ฒด๊ฐ Null์ด ์๋ ๊ฒฝ์ฐ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํฉ๋๋ค.
|
|
|
|
|
public void whenAssertingNull_thenTrue() { |
|
|
Object cat = null; |
|
|
|
|
|
assertNull(cat, "The cat should be null"); |
|
|
} |
assertNotNull์ ๊ฐ์ฒด๊ฐ Null์ด ์๋์ง๋ฅผ ํ์ธ ํฉ๋๋ค.
๊ฐ์ฒด๊ฐ Null์ผ ๊ฒฝ์ฐ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํฉ๋๋ค.
|
|
|
|
|
void whenAssertingNotNull_thenTrue() { |
|
|
Object dog = new Object(); |
|
|
|
|
|
assertNotNull(dog, "The dog should not be null"); |
|
|
} |
assertSame and assertNotSame
assertSame์ ์์ ๊ฐ๊ณผ ์ค์ ๊ฐ์ด ๋์ผํ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋์ง ํ์ธ ํฉ๋๋ค.
|
|
|
|
|
public void whenAssertingSameObject_thenSuccessfull() { |
|
|
String language = "Java"; |
|
|
String MyLanguage = "Java"; |
|
|
|
|
|
assertSame(language, MyLanguage); |
|
|
} |
๋ฐ๋๋ก, assertNotSame์ ์์ ๊ฐ๊ณผ ์ค์ ๊ฐ์ด ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋์ง ํ์ธํฉ๋๋ค.
|
|
|
|
|
public void whenAssertingSameObject_thenSuccessfull() { |
|
|
String language = "Java"; |
|
|
String anotherLanguage = "JavaScript"; |
|
|
|
|
|
assertNotSame(language, anotherLanguage); |
|
|
} |
fail
ํ ์คํธ๋ฅผ ์คํจ ์ฒ๋ฆฌํ๋ฉฐ ์คํจ ๋ฉ์์ง๋ฅผ ํจ๊ป ์ถ๋ ฅํฉ๋๋ค.
๊ฐ๋ฐ์ด ์๋ฃ๋์ง ์์ ํ ์คํธ๋ฅผ ํ์ํ ๋ ์ ์ฉ
|
|
|
|
|
public void whenFailingATest_thenFailed() { |
|
|
// Test not completed |
|
|
fail("FAIL - test not completed"); |
|
|
} |
assertAll
assertAll์์ ์์ฑ๋ ๋ชจ๋ Assertion์ด ์คํ๋ฉ๋๋ค.
ํ ๋ฉ์๋์ assertํจ์๋ฅผ ์ฌ๋ฌ๊ฐ ์ ์ธ ํ ํ
์คํธ๋ฅผ ์งํํ ์ ์๋จ assertํจ์์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ๊ทธ ๋ฐ ํจ์๋ค์ ์คํ๋์ง๊ฐ ์์ต๋๋ค.
ํ์ง๋ง, assertAll()๋ก ํด๋น ์๋ค์ ๋ฌถ์ ์ ์ค๊ฐ์ assertํจ์๊ฐ ์คํ์ด ๋์ง ์๋๋ผ๋ assert์ ๋ชจ๋ ํจ์๋ฅผ ์คํํ ์ ์์ต๋๋ค.
๋จ, ์คํ ์ค ์์ธ(Out Of Memory Error ๋ฑ)๋ฅผ ๋ฐ์์ํค๋ ๊ฒฝ์ฐ์๋ง ์ค๋จ๋ฉ๋๋ค.
|
|
|
|
|
void givenMultipleAssertion_whenAssertingAll_thenOK() { |
|
|
Object obj = null; |
|
|
assertAll( |
|
|
"heading", |
|
|
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"), |
|
|
() -> assertEquals("java", "JAVA".toLowerCase()), |
|
|
() -> assertNull(obj, "obj is null") |
|
|
); |
|
|
} |
assertIterableEquals
์์๋ ๋ฐ๋ณต ๊ฐ๋ฅ ํญ๋ชฉ๊ณผ ์ค์ ๋ฐ๋ณต ๊ฐ๋ฅ ํญ๋ชฉ์ด ์์ ํ ๋์ผํ์ง ํ์ธํฉ๋๋ค.
๋ Iterable์ ๋์ผํ ์์๋ก ๋์ผํ ์์๋ฅผ ๋ฐํํด์ผ ํ๋ฉฐ, ๋ Iterable์ด ๋์ผํ ์ ํ์ผ ํ์๋ ์์ต๋๋ค.
|
|
|
|
|
void givenTwoLists_whenAssertingIterables_thenEquals() { |
|
|
Iterable<String> al = new ArrayList<>(asList("Java", "Junit", "Test")); |
|
|
Iterable<String> ll = new LinkedList<>(asList("Java", "Junit", "Test")); |
|
|
|
|
|
assertIterableEquals(al, ll); |
|
|
} |
assertLinesMatch
์์ ๋ชฉ๋ก์ด ์ค์ ๋ชฉ๋ก๊ณผ ์ผ์นํ๋์ง ํ์ธํฉ๋๋ค.
์ด ๋ฉ์๋๋ assertEquals ๋ฐ assertItableEquals์๋ ๋ค๋ฆ ๋๋ค.
- ์์ ๋ผ์ธ์ด ์ค์ ๋ผ์ธ๊ณผ ๋์ผํ์ง ํ์ธ. ๋์ผํ ๊ฒฝ์ฐ ๋ค์ ์์ผ๋ก ๊ณ์ ์งํ
- ์์๋๋ ํ์ ์ ๊ท ํํ์ผ๋ก ์ฒ๋ฆฌํ์ฌ String.matches() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ฒดํฌ๋ฅผ ์ํ. ๋์ผํ ๊ฒฝ์ฐ ๋ค์ ์์ผ๋ก ๊ณ์ ์งํ
- ์์๋๋ ๋ผ์ธ์ด fast-forward marker ํ์ธ.๋์ผํ ๊ฒฝ์ฐ fast-forward๋ฅผ ์ ์ฉํ๊ณ ์คํ 1๋ถํฐ ์๊ณ ๋ฆฌ์ฆ์ ๋ฐ๋ณต
|
|
|
|
|
void whenAssertingEqualityListOfStrings_thenEqual() { |
|
|
List<String> expected = asList("Java", "\\d+", "JUnit"); |
|
|
List<String> actual = asList("Java", "11", "JUnit"); |
|
|
|
|
|
assertLinesMatch(expected, actual); |
|
|
} |
assertThrows
ํน์ ์์ธ๊ฐ ๋ฐ์ํ๋์ง ํ์ธํฉ๋๋ค.
์ฒซ ๋ฒ์งธ ์ธ์๋ "ํ์ธํ ์์ธ ํด๋์ค", ๋ ๋ฒ์งธ ์ธ์๋ "ํ ์คํธํ๋ ค๋ ์ฝ๋"๋ฅผ ์ ๋ ฅํฉ๋๋ค.
|
|
@Test |
|
|
void whenAssertingException_thenThrown() { |
|
|
Throwable exception = assertThrows( |
|
|
IllegalArgumentException.class, |
|
|
() -> { |
|
|
throw new IllegalArgumentException("Exception message"); |
|
|
} |
|
|
); |
|
|
assertEquals("Exception message", exception.getMessage()); |
|
|
} |
assertTimeout and assertTimeoutPreemptively
assertTimeout์ ํน์ ์๊ฐ ์์ ์คํ์ด ๋๋๋์ง ํ์ธ ํฉ๋๋ค.
assertTimeoutPreemptively์ ์ง์ ํ ์๊ฐ ๋ด ๋๋์ง ์์ผ๋ฉด ๋ฐ๋ก ์ข ๋ฃํฉ๋๋ค.
์๋ ์ฝ๋๋ 2์ด ์ด๋ด์ ๊ทธ ๋ค์ ์ธ์์ธ "Executable ์ฝ๋"๊ฐ ์คํ์๋ฃ ๋์ด์ผ ํฉ๋๋ค.
์ฌ๊ธฐ์ ์คํํ ์ฝ๋๋ Thread.sleep(1000) ์ผ๋ก, 1์ด๋์ ์ผ์ ์ ์ง ์ํ๊ฐ ๋ฉ๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ 2์ด ์ด๋ด์ ์ฝ๋๊ฐ ์คํ๋์ด ํ ์คํธ๋ ํต๊ณผํ๊ฒ ๋ฉ๋๋ค.
๋ง์ฝ Thread.sleep(2000), Thread.sleep(3000) ์ผ๋ก ์คํํ๊ฒ ๋๋ค๋ฉด 2์ด ์ด๋ด์ธ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ๋ชปํ๋ฏ๋ก ์คํจํ๊ฒ ๋ฉ๋๋ค.
|
|
@Test |
|
|
void whenAssertingTimeout_thenNotExceeded() { |
|
|
assertTimeout( |
|
|
ofSeconds(2), |
|
|
() -> { |
|
|
Thread.sleep(1000); |
|
|
} |
|
|
); |
|
|
} |
๐งช AssertJ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
AssertJ ๋ JUnit Assertions์ ์ฌ์ฉํจ์ ์์ด ๋ ์ฐ์ํ(?) ํ
์คํธ ์์ฑ์ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์
๋๋ค ๐
spring-boot-starter-test ์ ํฌํจ๋์ด ์์ต๋๋ค ๐ช
Hamcrest์ ๋น์ทํ ๊ธฐ๋ฅ์ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
๋ํ ํต์ฌ ๋ฉ์๋๋ assertThat 1๏ธโฃ ๊ฐ ์ด๋ฉฐ ๋ฉ์๋ ์ฒด์ด๋ ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค.
๐ก Object Assertions
๋ ๊ฐ์ฒด์ ๋์ผ์ฑ์ด๋ ๊ฐ์ฒด์ ํ๋๋ฅผ ์กฐ์ฌํ๊ธฐ ์ํด ์ฌ์ฉ
Example
public class Dog {
private String name;
private Double weight;
}
@Test
public class DogTest() {
Dog dog01 = new Dog("์ง๋๊ฐ", 3.45);
Dog dog02 = new Dog("์์ถ" , 1.23);
assertThat(dog01).isEqualTo(dog02);
}
isEqualsTo() ๋ ๊ฐ์ฒด์ ๋์ผ์ฑ์ ๋น๊ตํ๋ฏ๋ก ํด๋น ํ
์คํธ๋ fail์
๋๋ค ๐ฅฒ
๋๋ฑ์ฑ์ ๋น๊ตํ๊ธฐ ์ํด์๋ isEqualToComparingFieldByFieldRecursively() ์ ์ฌ์ฉํฉ๋๋ค.
๐ก Boolean Assertions
๋ ๊ฐ์ฒด์ Boolean ๊ฐ ๋น๊ต๋ฅผ ์ํด ์ฌ์ฉ
- isTrue()
- isFalse()
Example
assertThat("".isEmpty()).isTrue(); // true
๐ก Iterable/Array Assertions
Iterable/Array ์ ํน์ ์์๊ฐ ์กด์ฌํ๋์ง ํ์ธ ๋ฑ์ ๋ค์ํ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ
List<String> list = Arrays.asList("1","2","3");
assertThat(list).contains("1"); // ํฌํจ๋์๋์ง ํ์ธ
assertThat(list).isNotEmpty(); // ๋น์ด์๋์ง ํ์ธ
assertThat(list).startsWith("1"); // ํด๋น ์์๋ก ์์ํ๋์ง ํ์ธ
assertThat(list).doesNotContainNull() // null ํฌํจ ํ์ง ์๋์ง ํ์ธ
์ด์ธ์๋ AssertJ ๋ฅผ ์ด์ฉํด์ ๋ค์ํ Case๋ฅผ ์ง๊ด์ ์ผ๋ก ํ ์คํธ ํ ์ ์์ต๋๋ค ๐ช
๐งช ๋ง๋ฌด๋ฆฌ
JUnit5๋ ์๋ฐ๋ฅผ ์ด์ฉํ๋ ๊ฐ๋ฐ์๋ผ๋ฉด ๋น์ฐํ ์์งํด์ผํ ํ
์คํ
๊ธฐ๋ฒ์
๋๋ค ๐
ํนํ ์์ฆ ๊ฐ๋ฐ ๋ฌธํ์ TDD ๋ ์ค์ํ ์์๋ก ์๋ฆฌ ์ก์๊ธฐ์ ๋์ฑ ํ์์ ์ผ๋ก ์ตํ์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค ๐๐ป
'๊ฐ๋ ์ ๋ฆฌ(JAVA)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๊ฐ๋น์ง ์ปฌ๋ ์ (Garbage Collection) (0) | 2024.03.28 |
|---|---|
| ์๋ฐ ์์ฝ์ด ์ ๋ฆฌ (1) | 2024.03.28 |
| Test & TDD (0) | 2024.03.28 |
| ์ด์ง๊ฒ์ (2) | 2024.03.27 |
| ๊ฒ์ ์๊ณ ๋ฆฌ์ฆ (0) | 2024.03.27 |