728x90
반응형
[Spring framework] 데이터베이스 연결 테스트 코드
다음 테스트 코드들을 활용하여 데이터베이스 연결을 테스트합니다.
JDBCTests 클래스
package com.project.persistence;
import static org.junit.Assert.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
import lombok.extern.log4j.Log4j;
@Log4j
public class JDBCTests {
static {
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch(Exception e) {
e.printStackTrace();
}
}
@Test
public void testConnection()
{
try (Connection connection = DriverManager.getConnection(
"jdbc:mariadb://{IP Address}:{Port Number}/{Database Name}",
"{USERNAME}",
"{PASSWORD}"
)) {
log.info(connection);
if (connection != null) {
System.out.println("DB Connection Success!");
}
} catch (Exception e) {
fail(e.getMessage());
}
}
}
Xml Config 데이터베이스 연결 테스트 코드 : DataSourceTests 클래스
package com.project.persistence;
import static org.junit.Assert.fail;
import java.sql.Connection;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class DataSourceTests {
@Setter(onMethod_ = { @Autowired })
private DataSource dataSource;
@Setter(onMethod_ = {@Autowired})
private SqlSessionFactory sqlSessionFactory;
@Test
public void testMyBatis() {
try (
SqlSession session = sqlSessionFactory.openSession();
Connection con = dataSource.getConnection();
)
{
log.info(session);
log.info(con);
} catch (Exception e) {
// TODO Auto-generated catch block
fail(e.getMessage());
}
}
/*
@Test
public void testConnection() {
try (Connection con = dataSource.getConnection()){
log.info(con);
} catch (Exception e) {
// TODO Auto-generated catch block
fail(e.getMessage());
}
}
*/
}
Java Config 데이터베이스 연결 테스트 코드 : DataSourceTests 클래스
package com.project.persistence;
import static org.junit.Assert.fail;
import java.sql.Connection;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.project.config.RootConfig;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@ContextConfiguration(classes = {RootConfig.class}) // Java 설정
@Log4j
public class DataSourceTests {
@Setter(onMethod_ = { @Autowired })
private DataSource dataSource;
@Setter(onMethod_ = {@Autowired})
private SqlSessionFactory sqlSessionFactory;
@Test
public void testMyBatis() {
try (
SqlSession session = sqlSessionFactory.openSession();
Connection con = dataSource.getConnection();
)
{
log.info(session);
log.info(con);
} catch (Exception e) {
// TODO Auto-generated catch block
fail(e.getMessage());
}
}
/*
@Test
public void testConnection() {
try (Connection con = dataSource.getConnection()){
log.info(con);
} catch (Exception e) {
// TODO Auto-generated catch block
fail(e.getMessage());
}
}
*/
}
728x90
반응형
'java, spring' 카테고리의 다른 글
[Spring] 영속 계층의 CRUD 구현을 위한 환경설정 (0) | 2021.06.19 |
---|---|
[Spring framework] 스프링 MVC 프로젝트 Java로 구성하기 (0) | 2021.06.18 |
[Spring Framework] SQLSessionFactory (0) | 2021.06.11 |
[Spring framework] 스프링 MVC 프로젝트의 기본 구성 (0) | 2021.06.07 |
[Spring Framework] 404 에러 페이지 (0) | 2021.05.30 |