실전 개발자를 위한 Spring Framework Day17 실습 (2회차)
1. article-mapper.xml 테스트
- ArticleDAOTest.java를 활용해 테스트 수행
@Test
public void testInsertArticle() {
Article article = new Article(100, "tester", "tester", "tester");
dao.insertArticle(article);
}
//ArticleDAOTest전체 코드
package kr.co.company.hello.dao;
import kr.co.company.hello.vo.Article;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) //Spring-Test 사용 설정
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/*.xml") //설정파일 위치
public class ArticleDAOTest {
@Autowired
private ArticleDAO dao;
@Test
public void testInsertArticle() {
Article article = new Article(100, "tester", "tester", "tester");
dao.insertArticle(article);
}
@Test
@Ignore //테스트에서 제외
public void testSelectArticleById(){
Article article = dao.selectArticleById(null);
Assert.assertTrue(article.getAuthor().equals("lee"));
}
}
- 오류 발생: java.lang.classnotfoundexception: com.sql.jdbc.Driver
- datasource.xml 파일의 driverClass를 com.mysql.jdbc.Driver로 변경
- 오타 수정(spirng -> spring)
- sqlSessionFactory > mapperLocations 프로퍼티에 value 추가(classpath:mappers/article-mapper.xml)
<!-- <property name="driverClass" value="com.sql.jdbc.Driver"></property> -->\
<!-- <property name="url" value="jdbc:mysql://localhost:3306/spirng"></property> -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:property-placeholder location="classpath:config/database.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<!--
<property name="driverClass" value="${db.driverClass}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
-->
<!-- <property name="driverClass" value="com.sql.jdbc.Driver"></property> -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="spring"></property>
<property name="password" value="spring"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property> <!-- dataSource 아이디 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- config 파일 경로 -->
<property name="mapperLocations"> <!-- mapper 파일 경로 -->
<list>
<value>classpath:mappers/article-mapper.xml</value>
</list>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
- pom.xml에 mysql 추가
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
=> 테스트 성공
- select도 테스트 수행
@Test
public void testSelectArticleById(){
Article article = dao.selectArticleById("2");
System.out.println(article);
Assert.assertTrue(article.getAuthor().equals("lee"));
}
=> 성공