SQL server stub for java

ぃ、小莉子 提交于 2019-12-05 09:27:45

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.

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.

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.

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?

Ed Staub

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?

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=)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!