E D R , A S I H C RSS

Test Driven Database Development

TDD �� Database Programming �� ������ �� & 경����.
See Also TdddArticle

1002�� 경�� TDD �� DB ���� ����������까 궁������ ����� ������. �� TDD�� DB ���� ����� DB Repository ���� �� MockObject �� ������, ���� Mock �� ����고 ������. �� �� ����까�� �각��.
~cpp 

import junit.framework.TestCase;

import java.sql.*;

public class SpikeRepositoryTest extends TestCase {
    private Connection con;
    protected IRepository repository;
    private String writer;
    private String title;
    private String body;

    public void setUp() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        initConnection();
        repository= new SpikeRepository(con);
        repository.initialize();

        writer = "writer";
        title = "title";
        body = "body";
    }

    public void tearDown() throws SQLException {
        repository.destroy();
        uninitConnection();
    }

    public void testEdit() throws SQLException {
        repository.createArticle(writer, title, body);
        String writerEdited = "writerEdited";
        String titleEdited = "titleEdited";
        String bodyEdited = "bodyEdited";
        repository.edit(1, writerEdited, titleEdited, bodyEdited);
        Article article = repository.get(1);
        assertEquals (writerEdited, article.getWriter());
        assertEquals (titleEdited, article.getTitle());
        assertEquals (bodyEdited, article.getBody());
    }

    public void testTotalArticle() throws SQLException {
        repository.createArticle(writer, title, body);
        assertEquals (1, repository.getTotalArticle());
    }

    public void testCreateArticle() throws SQLException {
        repository.createArticle(writer, title, body);
        assertEquals (1, repository.getTotalArticle());
    }

    public void testDelete() throws SQLException {
        repository.createArticle(writer, title, body);
        repository.delete(1);
        assertEquals (0, repository.getTotalArticle());
    }

    public void testGet() throws SQLException {
        repository.createArticle(writer, title, body);
        Article article2 = repository.get(1);
        assertEquals(writer, article2.getWriter());
    }

    public void testArticleTableInitialize() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        repository = new SpikeRepository(con);
        repository.initialize();

        String articleTableName = "articlelist";

        String sqlStr="select id, writer, title, body from " + articleTableName;
        PreparedStatement pstmt= con.prepareStatement(sqlStr);
        ResultSet rs = pstmt.executeQuery();

        assertEquals (0, rs.getRow());
    }

    private void initConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        String hostname = "localhost";
        String dbname = "reset";
        String userId = "reset";
        String userPass = "reset";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String url="jdbc:mysql://"+hostname+"/"+dbname+"?user="+userId+"&password="+userPass;
        con = DriverManager.getConnection(url);
    }

    private void uninitConnection() throws SQLException {
        con.close();
    }

    public void testDuplicatedInitialize() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        repository.initialize();
        repository.initialize();
        repository.initialize();
        repository.destroy();
    }
}

����������, DB�� ���� ������ ���� ����� �������� ��������. (�� ���� ��. DB �� ���������� interface �� �������� DB ������ 계�� �������� DB ������ ���� ����.) ���� DB �� ���� ������ ������ side-effect� ����, �������� 2���� �������� side-effect�� ���� ����. ���������� initialize ������ destroy �������� ����고 �� setUp, tearDown �� �������� ���������� side-effect�� ��갔��.

����그������ ���, ���� ��기���� interface �� �������� 거꾸�� MockRepository �� ���� �� ����까 �� �각�� ����. (interface �� ���������� � �������� ���� ��격�� Repository, �� File Based �� ���� ���� ���� ������ ���� Repository �� ������ �각�� �� ���� ���.)
결과�� ������ ����� ����. ����, interface �� DB Exception ������� �������.
~cpp 

import java.sql.SQLException;

public interface IRepository {
    Article get(int index) throws SQLException;

    void initialize() throws SQLException;

    void destroy() throws SQLException;

    void edit(int index, String writer, String title, String body) throws SQLException;

    int getTotalArticle() throws SQLException;

    void createArticle(String writer, String title, String body) throws SQLException;

    void delete(int index);
}
��, MockRepository ������ Exception �� ���� ��� ������, ���������� �� throw �� ��������. (��������, ���� �������� �������� Java ������ Checked Exception �� ���������� ��.

���� MockRepository�� �� ������? interface �� ������간���� ����� ��겠��, ������ DBRepository �� ������ ����� ����. interface �� �������� ���� �� ����, interface �� ���� ����, �� SQL ��� Exception �� �� �� ������ ���� try-catch �� ����. ��, Database ������ ���������� �� Repository ���� ���� �게�� ����� ����� ������.

�� � ������ ����까. --1002

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:11
Processing time 0.0224 sec