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 ��������������� ���������������������.
��������� ��������� ��������� ������하������. ������, 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