Junit 테스트

이지윤·2022년 4월 6일
0
post-thumbnail

Unit 을 이용한 스프링 유닛 테스트

스프링 테스트

  • 스프링 프레임워크에서 만든 클래스(@Controller, @Service, @Repository,
    @Component 등이 붙은 클래스)를 테스트 하는 모듈

  • 단위 테스트, 통합 테스트를 지원하기 위한 매커니즘이나 편리한 기능을 제공

    • Junit 테스트 프레임워크를 사용 -> 스프링 DI 컨테이너를 동작시키는 기능
    • 트랜잭션 테스트를 상황에 맞게 제어하는 기능
    • 애플리케이션 서버를 사용하지 않고 스프링 MVC 동작을 재현하는 기능
    • RestTemplate 이용 -> HTTP 요청에 대한 임의 응답을 보내는 기능

스프링 테스트 - POM.XML 설정

<dependency> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> <scope>test</scope>
</dependency>
<dependency> <groupId>junit</groupId>
<artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope>
</dependency>

Junit 구성

업로드중..

테스트 케이스 작성 - MemberTest.java

  • memberService.readMember("hansol"); 테스트
package org.tukorea.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.tukorea.di.domain.StudentVO;
import org.tukorea.di.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:/applicationContext.xml")

public class MemberTest { @Autowired
 MemberService memberService;

@Test
public void testReadMember( ) throws Exception {
StudentVO member = memberService.readMember("hansol");
System.out.println(member);
}
}

다른 테스트 케이스

package org.tukorea.test;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.tukorea.di.domain.StudentVO;
import org.tukorea.di.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")

public class MemberTest {
	
	@Autowired
	MemberService memberService;
	
	//@Test
	public void testAddMember( ) throws Exception {
		String strID = "JUnit1";
		StudentVO vo = new StudentVO(); 
		vo.setId(strID); 
		vo.setPasswd(strID);  
		vo.setUsername(strID); 
		vo.setSnum(strID);
		memberService.addMember(vo);
		StudentVO member = memberService.readMember("JUnit");
		System.out.println(member);
	}
	
	@Test
	public void testReadMember( ) throws Exception {
		StudentVO member = memberService.readMember("jiyoon");
		System.out.println(member);
	}
}

DataSource 테스트 케이스

package org.tukorea.test;

import java.sql.Connection;

import org.slf4j.Logger;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class DataSourceTest {
	
	@Autowired
	private DataSource ds;
	
	private static Logger logger = LoggerFactory.getLogger(DataSourceTest.class);
	
	@Test 
	public void testConntection() throws Exception {
		try(Connection con = ds.getConnection()) {
			logger.info("con.toString() : " + con.toString());
		}catch(Exception e) {
			e.printStackTrace();
			
		}
	}
}

JdbcTemplate - 테스트 케이스

package org.tukorea.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class JdbcTemplateTest {
	
	
	@Autowired 
	private JdbcTemplate jt;
	
	
	private static Logger logger =LoggerFactory.getLogger(DataSourceTest.class);
	
	@Test 
	public void selectQuery() throws Exception {
		try {
			int count = jt.queryForObject("SELECT COUNT(*) FROM STUDENT", Integer.class);
			logger.info("Count :" + count);
			
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}
profile
초보자

0개의 댓글