스프링 프레임워크(24) JUnit & Spring-Test

넙데데맨·2022년 6월 16일
0
post-thumbnail

JUnit

단위 테스트 도구로 외부 테스트 프로그램(케이스)를 작성하여 System.out으로 디버깅하지 않아도 된다.
보이지 않는 숨겨진 단위 테스트를 끌어내어 정형화 시켜 주는 테스트용 Framework
JDK 1.4에서 추가된 asser 메소드를 통해 Test를 진행하며 최적화 코드 유추 기능 또한 제공한다.

테스트 지원 어노테이션

@Test

@Test가 선언된 메소드는 테스트를 수행하는 메소드가 된다.
Junit은 각각 테스트가 독립적 실행되는 것이 원칙임으로 @Test마다 객체를 생성

@Ignore

@Ignore가 선언된 메소드는 테스트를 실행하지 않게 한다.

@Before

@Test 메소드 실행마다 실행
@Test 메소드에서 공통 사용하는 코드를 @Before에 선언해 사용

@After

@Test메소드 실행마다 실행

@BeforeClass

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

@AfterClass

@Test보다 나중에 한 번만 수행되어야 할 경우에 사용

assert 메소드

테스트 결과를 확인하는 용도

assert 메소드 예시

assertEquals(a,b) 객체 a,b가 일치함 확인(값 동일)
assertArrayEquals(a,b) 배열 a,b가 일치함 확인
assertSame(a,b) 객체 a,b가 같은 객체임을 확인(레퍼런스 동일)
assertTrue(a) 조건 a가 참인지 확인
assertNotNull(a) 객체 a가 null이 아닌지 확인

Spring-Test

jUnit을 확장한 스프링의 테스트 라이브러리

테스트 지원 어노테이션

@RunWith

JUnit 프레임워크의 테스트 실행방법 확장 시 사용
@RunWith(SpringJUnit4ClassRunner .class)
SpringJUnit4ClassRunner 클래스 지정 시 JUnit이 테스트 진행하는 중에 ApplicationContext를 만들고 관리하는 작업을 진행 해준다.
각각 테스트 별로 객체가 생성되도 Singleton의 ApplicationContext를 보장한다.

@ContextConfiguration

스프링 빈 설정 파일의 위치를 지정할 때 사용

@Autowired

스프링 DI에서 사용
해당 변수에 자동으로 빈 매핑

JUnit 실습

HelloBeanJUnitTest.java

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame; // Assert 부분을 일일히 입력하지 않기 위해 static으로 import

public class HelloBeanJUnitTest {
	ApplicationContext context;
	@Before
	public void init() {
		// 1. IoC 컨테이너 생성
		//ApplicationContext 객체 생성
		context = new GenericXmlApplicationContext("config/beans.xml");
	}
	
	@Test
	public void test1() {

		// 2. Hello Bean 가져오기 
		//Object 타입으로 가져오기 때문에 Hello 객체로 형변환 필요
		Hello hello =(Hello)context.getBean("hello");
		assertEquals("Hello Spring",hello.sayHello());
		hello.print();
		// 3. StringPrinter Bean 가져오기
		Printer printer = (Printer)context.getBean("printer");
		assertEquals("Hello Spring",printer.toString());

	}
	
	@Test @Ignore
	public void test2() {

		Hello hello =(Hello)context.getBean("hello");
		Hello hello2 =(Hello)context.getBean("hello");
		assertSame(hello,hello2);
	}

}

Spring-Test 실습

HelloBeanJUnitTest.java

package myspring.di.xml.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import myspring.di.xml.Hello;
import myspring.di.xml.Printer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/beans.xml")
public class HelloBeanJUnitSpringTest {
	@Autowired
	ApplicationContext context;

	@Test
	public void test1() {

		// 2. Hello Bean 가져오기 
		//Object 타입으로 가져오기 때문에 Hello 객체로 형변환 필요
		Hello hello =(Hello)context.getBean("hello");
		assertEquals("Hello Spring",hello.sayHello());
		hello.print();
		// 3. StringPrinter Bean 가져오기
		Printer printer = (Printer)context.getBean("printer");
		assertEquals("Hello Spring",printer.toString());

	}
	
	@Test @Ignore
	public void test2() {

		Hello hello =(Hello)context.getBean("hello");
		Hello hello2 =(Hello)context.getBean("hello");
		assertSame(hello,hello2);
	}

}
profile
차근차근

0개의 댓글