[인강정리] 05. DI 애플리케이션 작성(2) - jUnit

[인강정리] 05. DI 애플리케이션 작성(2) - jUnit

1. jUnit의 특징

- 단정(assert) 메서드로 테스트 케이스의 수행 결과를 판별.

- jUnit4부터는 간단하게 테스트를 진행할 수 있도록 어노테이션을 제공한다. @Test, @Before @After

- 각 @Test 메서드가 호출될 때마다 새로운 인스턴스를 생성하여 독립적인 테스트가 이루어지도록 한다.

1-2. jUnit 라이브러리 설치

- pom.xml에 추가해준다. (spring mvc project 템플릿엔 기본 포함되어 있음.)

2. jUnit의 어노테이션

1) @Test

- 테스트를 수행하는 메소드에 지정.

- 각각의 테스트가 서로 영향을 주지 않고 독립적으로 실행됨을 원칙으로 함. -> 각각의 @Test마다 객체를 생성함

2) @Ignore

- 이것이 선언된 메서드느 테스트를 실행하지 않게 한다.

3) @Before

- 이것이 선언된 메서드는 @Test 메소드가 실행되기 전에 반드시 실행된다.

- @Test 메서드에서 공통으로 사용하는 코드를 @Before 메서드에 선언하여 사용하면 됨.

4) @After

- 이것이 선언된 메서드는 @Test 메서드가 실행된 후 실행된다.

5) @BeforeClass

- @Test 메서드보다 먼저 '한번만' 수행되어야 할 경우에 사용

6) @AfterClaass

- @Test 메서드 실행 이후에 '한번만' 수행되어야 할 경우에 사용.

2-1 테스트 결과를 확인하는 단정(assert) 메서드

1) assertEquals(a,b)

- 객체 A와 B의 값이 일치하는지를 확인

2) assertArrayEquals(a,b)

- 배열 A,B가 일치함을 확인

3) assertSame(a,b)

- 객체 A와 B가 같은 레퍼런스인지 확인

4) assertTrue(a)

- 조건 A가 참인지 확인

5) assertNotNull(a)

- 객체 A가 null이 아님을 확인

3. Spring-Test를 활용한 테스트

3-1 Sprint-Test의 어노테이션

1) @RunWith

- jUnit 프레임워크의 테스트 실행방법을 확장할 때 사용하는 어노테이션

- SpringUnit4ClassRunner라는 클래스를 지정해주면 jUnit이 테스트를 진행하는 중에 ApplicationContext를 만들고 관리하는 작업을 진행해 줌.

- 각각의 테스트 별로 객체가 생성되더라도 싱글톤의 ApplicationContext를 보장함.

2) @ContextConfiguration

- 스프링 빈 설정 파일의 위치를 지정해 줌.

3) @Autowired

- 해당 변수에 자동으로 빈을 매핑해줌.

===> 이를 이용하면 굳이 GenericXmlApplicationContext를 사용할 필요가 없다.

import static org.junit.Assert.assertEquals; public class HelloBeanJunitTest { ApplicationContext ctx; @Before public void init() { ctx = new GenericXmlApplicationContext("classpath:config/beans.xml"); } @Test public void test1() { Hello hello = (Hello) ctx.getBean("hello"); assertEquals("Hello Spring", hello.sayHello()); //import static org.junit.Assert.assertEquals; 해주면 Assert. 생략가능 hello.print(); Printer printer = (Printer) ctx.getBean("printer"); assertEquals("Hello Spring", printer.toString()); } @Test public void test2() { Hello hello = (Hello)context.getBean("hello"); Hello hello2 = (Hello)context.getBean("hello"); assertSame(hello, hello2); } }

Junit의 test 예제이다.

package spring.di; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:config/beans.xml") public class HelloBeanJunitTest { @Autowired ApplicationContext ctx; @Test public void test1() { Hello hello = (Hello) ctx.getBean("hello"); assertEquals("Hello Spring", hello.sayHello()); //import static org.junit.Assert.*; 해주면 Assert. 생략가능 hello.print(); Printer printer = (Printer) ctx.getBean("printer"); assertEquals("Hello Spring", printer.toString()); } @Test public void test2() { Hello hello = (Hello)context.getBean("hello"); Hello hello2 = (Hello)context.getBean("hello"); assertSame(hello, hello2); } }

spring-test의 예제이다.

from http://purewater-practice-diary.tistory.com/59 by ccl(A) rewrite - 2021-12-24 21:02:00