1

I start new project and I want to use TDD methodology. I use tomcat 7 and mysql 5. I wonder how to test connection with DB? Thanks in advance

nyanev
  • 11,299
  • 6
  • 47
  • 76
  • what language are you planning use for your new project? – questborn Nov 21 '11 at 23:07
  • Java, I assume, based on it saying so in the title. – Gian Nov 21 '11 at 23:10
  • 2
    An ideal option would be to use mock objects to mimic DB operations. Almost all DB interactions happen through standard interfaces, and you can use mock frameworks, like EasyMock, Mockito, to create mocks for those interfaces with desired behavior. – srkavin Nov 21 '11 at 23:13
  • 1
    This page should give you an idea of open source testing tools for Java.[link](http://java-source.net/open-source/testing-tools) – questborn Nov 21 '11 at 23:25

3 Answers3

5

Probably you don't have to test the connection to the DB. Even though if you follow TDD strictly to the letter you would need to write a test and only then write the connection, it is really a bit of an overkill. Of course, you should mock Cursor and all the objects that the DB is providing and use those in your tests; what you shouldn't test is the implementation of the connection itself, since you can assume it has been tested by the developers of the DB. Also, achieving 100% coverage, though ideal, may not be the most efficient use of your time. Probably those last points can be used by testing different paths in the code even though technically they are already in your coverage.

Luis
  • 1,294
  • 7
  • 9
2

Excellent initiative! You should start looking at mock objects and DB mocking. This SO question might help you to get started.

EDIT: In case you are looking for a mocking framework, I suggest EasyMock which is an excellent mocking framework that I have been using for more than 4 years now.

Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
0

I found DBUnit particularly useful for testing a DB project I did. This framework allows you to set up an in-memory database which can be configured prior to each test.

Martin
  • 7,089
  • 3
  • 28
  • 43