5

I have a java application that is using MSSQL server through the JDBC driver. Is there some kind of stub that I can use for testing? For example I want to test how my application handle cases of connection errors, SQL server out of disk, and other exceptions. It's pretty hard and complex to simulate this with real SQL server.

Thanks

duduamar
  • 3,816
  • 7
  • 35
  • 54
  • Hi, setting a bounty on that question, hoping to get more responses to it. As I commented below, I'd like a way to test my application externally as a black box, and not internally using unit tests. Is there a stub SQL server out there? Or what can be the easiest way to create one? Thanks in advance! – duduamar Jun 29 '11 at 21:49

6 Answers6

1

You could write unit tests against your DAOs or repositories returning mock Connection objects using a mock library such as https://mocquer.dev.java.net/.

You'd need a really clean and decoupled application architecture though in order to make this work correctly and provide you with actual test coverage.

cherouvim
  • 31,725
  • 15
  • 104
  • 153
  • A don't a mock for unit test, but for a full functional testing of my application. Ideally it will be some kind of stub that I can control, or "tell" it how to behave (for example - after 3 insertions to DB, start throwing out of disk space exceptions). – duduamar Feb 14 '11 at 18:05
  • @cherouvim - It looks like mocquer moved or is dead. The link is dead, it's not in the index at http://www.java.net/projects/community, and I can't find it elsewhere. Do you know where it's gone to? – Ed Staub Jul 06 '11 at 01:53
  • @Ed Staub: Yes it's sad. java.net has always been a crappy site and now that oracle runs it it's "FUBAR". I'd simply grab the jar and start working: http://www.google.com/search?q=mocquer+jar or look for another mock library. – cherouvim Jul 06 '11 at 02:19
1

You could (assuming the system is architected in a way to make this easy) create your own versions of the DB Access classes (I assume you are using teh statement/preparedstatement interfaces), which would hold the real DB calls and that you can modify to do exactly what you want.

I've done this - it takes a day or so of really boring work.

Carl
  • 11
  • 1
1

I don't think there's something like that.
You'd be better off setting up your own database and testing on your machine/lan.

All I know there is out there, is:

Both support MySQL, but none MS-SQL. I do think that has to do with licensing issues and limitations. So I'm afraid you won't find a similar service for MS-SQL db.

c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
0

Answering myself with an option I thought of, I'll be glad to hear your inputs on it. After crawling around, I got to HyperSQLDB, a java-implemented database. How feasible do you think is to take the source code of HSQLDB, and adding another layer to it, so I can control it and inject pre-defined behaviors to it. For example, I'll make it run all queries slowly, I'll make it disconnect, etc.

Do you think this idea is worth pursuing? Is it doable in a reasonable amount of time?

duduamar
  • 3,816
  • 7
  • 35
  • 54
0

If you use something other than MS-SQL, you may cause more testing problems due to incompatibilities and lack of functionality (e.g., transactions) than you solve. So I'm with Carl - use a shim.

If you were looking for unit-test coverage of ordinary behavior, I might think differently.

I haven't used them personally, but the stuff you're talking about sounds like a really good fit for a mocking framework, such as Mockito(docs) or PowerMock. They appear to provide good support for the kind of failure injection you're after. Can someone with experience with either of them (or similar) weigh in? See also How to stub/mock JDBC ResultSet to work both with Java 5 and 6?

Community
  • 1
  • 1
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
0

execute procedure sp_who2 it will generate the all the current connections and process in your db you can see a column named spid corresponding to each db connection. just type: kill <<spid>> and execute it to terminate any users..etc. but if the spid is less than 50 it means it is a system process and dont kill it. This can help you replicate connection drops. you can also say ALTER DATABASE dbname SET SINGLE_USER WITH ROLLBACK_IMMEDIATE this will drop all connections to the said db immediately.

Select @@MAX_Connections as Max_Connections would give you the max connections which can be made to a database (you can set it to a low number to test connection unavailability).

to replicate query timeout.. set the query timeout to a very low number & execute a fairly large query.

to create disk space error, simply redice the size of the db file & do not allow it to grow... then insert data to the database (you'll get an exception).

altert database xxx (file= maxsize= filegrowth=)

Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
  • How can I automate this? I need to write automated tests, not run this sequence manually every time. – duduamar Jul 06 '11 at 19:58
  • these are simple qureies... you could execute them while your program is running & get the desired results.. again this can be done through a program itself which clears all connections from time to time... etc. just write a thread which wakes up after a specific duration & runs these queries... – Anantha Sharma Jul 07 '11 at 04:36