์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- AndroidStudio
- di
- ์ฝ๋์
- Kotlin
- tcp
- ํ๋ IT&E
- udp
- 2024-08-20
- Factory Method Pattern
- 2024-08-21
- uri
- Python
- reflection
- OOP
- fontstyle
- ๊ธฐ์ด100์
- FACTORY
- ๊ฐ์ฒด์งํฅํ๋ก๊ทธ๋๋ฐ
- swagger
- URN
- menutab
- OpenAPI
- http method
- Android Studio
- ์ด๋ ธํ ์ด์
- ์ฑ์ฉํ์ ํ
- url
- Dialog
- IOC
- datepicker
dingdong coding
[ JUnit5 ] ์์ธ ํ ์คํธ ๋ณธ๋ฌธ
spring-boot-starter-web์ ์ ํํ๋ค๋ฉด ์๋์ผ๋ก spring-boot-starter-test ์์กด์ฑ์ด ์ถ๊ฐ๋์ด์์ต๋๋ค.
spring-boot-starter์ junit5๊ฐ ์์ด ๋ฐ๋ก junit5๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
build.gradle > dependecies
testImplementation 'org.springframework.boot:spring-boot-starter-test'
RuntimeException์ด ๋ฐ์ํ๋ ๋ฉ์๋
public class DoSomething {
public static void func() {
throw new RuntimeException("some exception message...");
}
}
1. assertThrows
Assertions.assertThrows์ ๋ ๋ฒ์งธ ์ธ์์ธ DoSomething.func()๋ฅผ ์คํํ์ฌ ์ฒซ ๋ฒ์งธ ์ธ์์ธ ์์ธ ํ์ ๊ณผ ๊ฐ์์ง(ํน์ ์บ์คํ ์ด ๊ฐ๋ฅํ ์์ ๊ด๊ณ์ ์์ธ์ธ์ง) ๊ฒ์ฌํฉ๋๋ค.
import static org.junit.jupiter.api.Assertions.assertThrows;
@Test
public void junit5์์_exception_ํ
์คํธ_1() {
Assertions.assertThrows(RuntimeException.class, () -> {
DoSomething.func();
});
}
2. try~catch๋ฌธ assertEquals
์์ธ ๋ฉ์ธ์ง๋ ๋ณํ๊ธฐ ์ฌ์ด ๊ฐ์ด๊ธฐ ๋๋ฌธ์ ํ ์คํธํ๋ฉด ๊นจ์ง๊ธฐ ์ฌ์ด ํ ์คํธ ์ฝ๋๊ฐ ๋ฐ์ํ๋ฏ๋ก ํ ์คํธ ํ์ง ์๋ ๊ฒ์ด ์ข์ต๋๋ค. ( ์ฝ๋ํ ์๋ต ๋ฉ์์ง์ ๊ฒฝ์ฐ์๋ ํ ์คํธํ ์ ์์ต๋๋ค. )
mport static org.junit.jupiter.api.Assertions.assertThrows;
@Test
public void junit5์์_exception_ํ
์คํธ_3() {
try {
DoSomething.func();
} catch (RuntimeException e) {
Assertions.assertEquals("some exception message...", e.getMessage());
}
}
3. assertThrows ๋ฐํ๊ฐ ์ฌ์ฉ
assertThrows๋ ๋ฐ์ํ๋ ์์ธ๋ฅผ ๋ชจ๋ ํด๋์ค์ ์ ์กฐ ํด๋์ค์ธ Throwable ํ์ ์ผ๋ก ๋ฐํํฉ๋๋ค.
Throwable๋ก ๋ฐํ๋ ๋ฉ์๋์์ ๋ฐ์ํ ์์ธ ๊ฐ์ฒด์ ๋ฉ์์ง๋ฅผ ํตํ์ฌ ์์ธ ๋ฉ์์ง ํ ์คํธ๋ฅผ ํ ์ ์์ต๋๋ค.
@Test
public void junit5์์_exception_ํ
์คํธ_4() {
Throwable exception = assertThrows(RuntimeException.class, () -> {
DoSomething.func();
});
assertEquals("some exception message...", exception.getMessage());
}
ํ์ต์ ๋ง์ ๋์์ด ๋ ๋ธ๋ก๊ทธ ๋งํฌ๋ฅผ ์ฐธ์กฐํ์ต๋๋ค. ๐
์ฐธ์กฐ ๋ฐ ์ถ์ฒ
'๐ชTEST Code' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
jest (0) | 2025.02.03 |
---|