Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test-driven development not working for my class

Tags:

java

tdd

I have this class that I wanted to build using TDD, but I failed. It's a pretty basic class called SubMissions, and all it does is it fetches some data from an SQL database.

So it has methods like getSubMissionForPage(), getSubMissionFromId() etc..

I tried building it using TDD. My first test contained a call to getSubMissionPage(), which only purpose is to return data. So making this test fail is pretty darn hard, since it can return any data, I couldn't come up with a way to make it fail.

I know making your test fail is the first step into knowing what to implement, but what do you do when there's actually no way of failing a test?

like image 286
Luca Matteis Avatar asked Nov 28 '10 22:11

Luca Matteis


People also ask

What are the major challenges faced when performing Test-Driven Development?

The main problem with Test-Driven Development, is that unit testing is not a measure of correctness but a measure of predictable behavior. Unit tests guarantee that our code behaves as we expected it to, but the expected behavior might be incorrect, incomplete or functional only on happy flows.

Why TDD is not usually used?

This means the following problems in such a TDD approach: More test code than the implementation code. Not easy to design tests before the implementation is done. Implementation refactoring breaks existing tests.

What happens if the initial test fails in Test-Driven Development?

In simple terms, test cases for each functionality are created and tested first and if the test fails then the new code is written in order to pass the test and making code simple and bug-free. Test-Driven Development starts with designing and developing tests for every small functionality of an application.


2 Answers

Any time you're relying on an external data source, your test can always fail. What if the connection is dropped to your DB? What if the table doesn't exist that you're trying to get data from? What if the data you get back is not the data you expect?

Testing classes that connect to DBs is a little more tricky than testing HelloWorld.java, because you need some way to mock the DB. You can learn more about Mock Objects here.

Short answer, your tests/software can ALWAYS fail. If you don't think your test can fail, you're not thinking hard enough about the problem space.

like image 112
Visionary Software Solutions Avatar answered Sep 30 '22 23:09

Visionary Software Solutions


Retrieval from a database isn't the place to start with TDD.

You might do well to look at some examples of TDD on the net. Bob Martin's bowling game scoring is a fun place to start.

That being said ...

My first test contained a call to getSubMissionPage(), which only purpose is to return data. So making this test fail is pretty darn hard, since it can return any data, I couldn't come up with a way to make it fail.

The purpose isn't to return data, but to return the correct data.

The way to make a test for this is to supply it with a database that should make it return a specific result and see that it does. And of course it won't until you write the correct code, so you write the test first and the real implementation after seeing it fail.

The hard part with TDD involving a database is that testing with a real database can be slow and making the test repeatable can be difficult as your tests will sometimes change the data. To deal with these issues, you can get help from tools like DbUnit, mock JDBC, use an in-memory database, and use rollbacks to make sure your tests don't make permanent changes.

But you're best off not starting with database stuff.

like image 23
Don Roby Avatar answered Sep 30 '22 23:09

Don Roby