rosieblue
article thumbnail
728x90

์˜ค๋Š˜ ๋ถ„์„ํ•  ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์˜ ์ •ํ™•ํ•œ ์›๋ฆฌ๊ฐ€ ๊ถ๊ธˆํ•ด์„œ assertThrows์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค. assertThrows๋Š” JUnit์—์„œ ์ œ๊ณตํ•˜๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ ์ค‘์˜ ํ•˜๋‚˜์ด๋‹ค.

//memberService.join(member2)ํ•˜๋ฉด IllegalStateException์ด ์ผ์–ด๋‚˜์•ผํ•œ๋‹ค๋Š” ๋œป
IllegalStateException e= Assertions.assertThrows(IllegalStateException.class, 
	                   ()->memberService.join(member2));
assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค.");

์ผ๋‹จ JUnit์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ํ•˜๋‹ˆ JUnit์ด ์–ด๋–ค ํ”„๋ ˆ์ž„์›Œํฌ์ธ์ง€๋งŒ ์•„์ฃผ์•„์ฃผ ์งง๊ฒŒ ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜์ž.

JUnit์ด๋ž€?

Java์—์„œ ๋…๋ฆฝ๋œ ๋‹จ์œ„ํ…Œ์ŠคํŠธ (Unit test)๋ฅผ ์ง€์›ํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

๊ทธ๋Ÿผ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋ž€?

ํŠน์ • ์†Œ์Šค์ฝ”๋“œ์˜ ๋ชจ๋“ˆ์ด ์˜๋„ํ•œ ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•˜๋Š” ํ…Œ์ŠคํŠธ์ด๋‹ค. ์ฆ‰ ํ•จ์ˆ˜ ๋ฐ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ํ…Œ์ŠคํŠธํ• ์ˆ˜ ์žˆ๋‹ค.

๊ฐ„๋‹จํžˆ ๋งํ•ด์„œ ๋‚ด๊ฐ€ ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ๊ทธ๋•Œ ๊ทธ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •๋ง ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋•Œ Junit์˜ @Test๋ฅผ ์ด์šฉํ•ด์„œ ํ•จ์ˆ˜(ํ˜น์€ ๋ฉ”์†Œ๋“œ) ํ•˜๋‚˜์”ฉ๋งŒ์„ ํ…Œ์ŠคํŠธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์Œ์— ๋‹ค์‹œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 


assertThrows()

static <T extends Throwable>T assertThrows(Class<T> expectedType, Executable executable)

์ด ๋ฉ”์†Œ๋“œ๋Š” ์ธ์ž๋กœ ์˜ˆ์ƒ๋˜๋Š” exception๊ณผ ์‹คํ–‰๋  ๋™์ž‘(Executable)์„ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š”๋‹ค.

๋ฐ›์€ ๋™์ž‘(Executable)์„ ์‹คํ–‰ํ•œ ํ›„ ๋ฐœ์ƒํ•˜๋Š” exception์ด ์ธ์ž๋กœ ๋ฐ›์•˜๋˜ exception๊ณผ ๋™์ผํ•˜๋ฉด ํ•ด๋‹น exception์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค. ์ฐ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

 

๋Œ€์ถฉ ์˜์‚ฌ ์ฝ”๋“œ๋กœ ์ •๋ฆฌํ•ด๋ณด์•˜๋Š”๋ฐ ์ด๋Ÿฐ ์‹์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

private static <T extends Throwable> T assertThrows((Class<T> expectedType, Executable executable, Object messageOrSupplier) {
	try{
    	executable ์‹คํ–‰}
    
    //์˜ˆ์™ธ๋ฐœ์ƒ
   	catch(์˜ˆ์™ธ){
    	if (๋ฐœ์ƒํ•œ ์˜ˆ์™ธ == ์ธ์ž๋กœ ๋ฐ›์€ ์˜ˆ์™ธ)
        {
        	return ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ
        }
        else {
        	throw ๋‹ค๋ฅธ ์˜ˆ์™ธ
        }
        
    //์˜ˆ์™ธ ์•ˆ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ
    throw (์˜ˆ์™ธ ์•ˆ๋ฐœ์ƒํ–ˆ๋‹ค๋Š” ์˜ˆ์™ธ)
}

 

 

๊ทธ๋Ÿผ ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž

//memberService.join(member2)ํ•˜๋ฉด IllegalStateException์ด ์ผ์–ด๋‚˜์•ผํ•œ๋‹ค๋Š” ๋œป
IllegalStateException e= Assertions.assertThrows(IllegalStateException.class, 
	                   ()->memberService.join(member2));

IllegalStateException ์˜ˆ์™ธ๊ฐ€ ๋‚˜ํƒ€๋‚  ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•˜๊ณ  ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  memberService.join(member2)๋ฅผ ํ•˜๋„๋ก ํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ Executable ์ธ์ž๋ฅผ ๋„˜๊ธธ ๋•Œ๋Š” ์ €๋Ÿฐ์‹์œผ๋กœ ๋ณดํ†ต ๋žŒ๋‹ค์‹์œผ๋กœ ๋„˜๊ธด๋‹ค! ( ๋žŒ๋‹ค์‹์—์„œ๋Š” ์ธ์ž ์—†์œผ๋ฉด ()->์–ด์ฉŒ๊ตฌ ํ˜•ํƒœ๋กœ ์”€)

์•„๋ฌดํŠผ ์ € ๋ฉ”์†Œ๋“œ๋ฅผ ๋Œ๋ ธ๊ณ  ๋‚ด๋ถ€์—์„œ ์‹ค์ œ๋กœ IllegalStateException์ด ์ผ์–ด๋‚ฌ๋‹ค๋ฉด ๋ฉ”์†Œ๋“œ๋Š”  IllegalStateException๋ฅผ returnํ•  ๊ฒƒ์ด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋‹ค๋ฅธ exception์„ returnํ•  ๊ฒƒ์ด๋‹ค. ์ด๋•Œ ๊ทธ exception์„ e์—  ๋Œ€์ž…ํ•œ๋‹ค.

 

 

์ดํ›„ e๋ฅผ ์–ด๋–ค ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ƒ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

assertThat(e.getMessage()).isEqualTo("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค.");

e์˜ ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค"์™€ ๊ฐ™์œผ๋ฉด e๊ฐ€ IllegalStateException์ด๋ผ๊ณ  ํŒ๋‹จํ•˜๊ณ  ํ…Œ์ŠคํŠธ๋Š” ์„ฑ๊ณตํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๊ฒŒ ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ์˜ˆ์ „์— IllegalStateException์ด ์ผ์–ด๋‚˜๋ฉด ์ €๋Ÿฐ ์—๋Ÿฌ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (์—ฌ๊ธฐ ์ฐธ๊ณ )

 

์ •๋ฆฌ
์˜ˆ์™ธ ๊ฐ์ฒด e = assertThrows(์˜ˆ์ƒ๋˜๋Š” ์˜ˆ์™ธ,์‹คํ–‰ํ•  ์•ก์…˜);
์˜ˆ์ƒ๋˜๋Š” ์˜ˆ์™ธ์˜ ํŠน์„ฑ๊ณผ e์˜ ํŠน์„ฑ ๋น„๊ต (์œ„์—์„œ๋Š” ์—๋Ÿฌ๋ฉ”์‹œ์ง€ ๋น„๊ตํ–ˆ์—ˆ์Œ) ํ•ด์„œ ๊ฒ€์ฆ

 

 

References

https://cheershennah.tistory.com/220

https://small-stap.tistory.com/77

profile

rosieblue

@Rosieblue

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!