본문 바로가기
Framework & Library/Spring Framework

[Spring Framework] : MySQL 연동 Test Code 작성 및 확인

by 오주현 2022. 1. 6.
반응형
MySQL 연동 Test Code 작성 및 확인

데이터 베이스 테스트 코드 작성


	<!-- MySQL-Connector -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>8.0.19</version>
		</dependency>
	
	
		<!-- junit Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
  • 테스크 코드 작성을 위해 MySQL-Connector 라이브러리와 junit 라이브러리를 pom.xml에 추가하고 Maven Update를 진행해 준다.

  • 프로젝트 아래 test 폴더를 생성하고 패키지 하나를 생성한뒤 MySQLConnectionTest 클래스를 생성해 준다.
package crudProject01;

import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.DriverManager;

import org.junit.Test;

public class MySQLConnectionTest {
	
	static {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testConnection() {
		
		try(Connection con = 
				DriverManager.getConnection(
						"jdbc:mysql://13.125.11.44/crudProject01?serverTimezone=Asia/Seoul",
						"root",
						"1234")){
			System.out.println(con);
		} catch (Exception e) {
			fail(e.getMessage());
		}
		
	}
}
  • junit과 MySQL Connection을 import하고 테스트 코드를 작성한다.
  • AWS IP 주소와 /Database 스키마 이름을 넣어주면 된다.

  • 코드 작성이 완료되었다면 Run을 눌러 JUnit Test를 선택해 주면 된다.
  • 결과를 보면 에러코드 없이 정상 작동한다.
  • 데이터 베이스 테스트에 성공했다. 
반응형

댓글