1

I have read all discussion in the stack overflow, but still this is unclear to me

I have 4 methods to test 1. Create 2. Update 3. Read 4. Delete

As per jUnit standarrds, a unit test is suppose to run independently in any order. But now my question is, Assume the database is empty , the test cases update, read, delete are absolutely useless or wont work as expected.

Now, whats the elegant way to handle this? Apart from running a init SQL script right in to the DB

madhairsilence
  • 3,787
  • 2
  • 35
  • 76
  • You can have multiple tests which each executes a given sequence of the four methods and checks if the database looks like it should at all times. – Thorbjørn Ravn Andersen Feb 06 '15 at 06:58
  • the word 'sequence' is what concerns me.. why should i define a sequence for junit.. its an unit , it should run independently – madhairsilence Feb 06 '15 at 07:00
  • 1
    that's the role of the setup method (@Before): Loading a database with a specific, known set of data (http://en.wikipedia.org/wiki/Test_fixture) – syllabus Feb 06 '15 at 07:11
  • @syllabus ok.. now we are talking.. So I use the setup method,.in order to load the DB , i need to use another method, for which the test case might have been completed yet.. so, is running SQL script is the only way?? – madhairsilence Feb 06 '15 at 07:16
  • 1
    of course you must not use another "to-be-tested-method" to setup the db. SQL is one way to do if you really want to test the full operation (your method, driver, jdbc stack, ...) but mind that it is not "unit" test. If you want to test only your logic, try to use mock objects. – syllabus Feb 06 '15 at 07:22
  • @madhairsilence you may have multiple junit test each consists of a sequence of the methods you want to test to see if the behaviour is as expected. You most likely cannot each of your methodsfully in isolation. – Thorbjørn Ravn Andersen Feb 06 '15 at 08:02
  • @syllabus Yeh.. so.. can I create a Mock Entity object and use it in the complete suite. This object is a Mock up of real database entry! something like that! – madhairsilence Feb 06 '15 at 08:52

0 Answers0