java - How would you approach TDD-ing a spring web application -


i'm looking advice on how approach creating spring web application (with hibernate) in pure tdd fashion. meaning shouldn't write production code without having failing unit test first.

would unit testing creation of application context? if yes how go doing that?

would easier tdd spring app when using java configuration instead of xml or annotation based configuration?

when write test needs spring applicationcontext , database, integration test, not unit test. general rules unit tests are:

  • they test 1 thing (i.e. method calls other classes / beans frowned upon)
  • they have lean setup (i.e. no loading of test data database, no transactions, no huge app context)

integration tests, on other hand:

  • are slow (create database connection, loading test data database, big setup step wire many beans, configure spring, ...)
  • brittle (because of many dependencies)
  • if fail, know it's somewhere in 500'000 lines of code executed.

so tdd, try build beans can create without spring. write them in such way don't have start hibernate or database. main reason here tdd needs run unit tests hundreds of times each day. if run longer 10 seconds, feel you're wasting time waiting tests finish.

the next question how can test useful way. well, think of way: hibernate works. has many unit tests. testing hibernate waste of time. should create layer in application hides hibernate code.

instead of wiring foodao, wire ifoodao has byid() method , returns pojo. in unit tests, can create mock implementation returns single instance.

when want know whether real foodao works, write integration tests call byid() few times.

but avoid "get object dao, process object, save object dao again". these 3 distinct tests (two it, 1 ut).


Comments