简体   繁体   中英

Java test driven development

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM