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.0248 sec