Can we execute SQL without having a real database connection in java? example:
SELECT CASE
WHEN :param = 1 THEN 'TEST1'
WHEN :param = 2 THEN 'TEST2'
WHEN :param = 3 THEN 'TEST3'
END
AS RESULT
FROM DUAL
I will replace :param in runtime in java code. Is there any way to do this?
I found this link: How do I extract selected columns given an Oracle SQL String?
but no quick solution is provided in this link
Currently thinking of: dummy hsqldb connection and execute SQL query. But it requires to span a new in memory db.
Is there any better & quick solution?
but why are you trying to do that? Is your intention to unit test your query? or the code that calls the query?
If you're trying to unit test database objects, you might be better of putting it into a proc and utilizing a database unit testing framework. Some info on oracle unit testing. I'm assuming oracle based on dual.
If you're trying to test your java code, you need to think about pulling the query directly out of the method you're attempting to test, and programming to an interface to which you can provide "real" implementation, and a mock or fake implementation. This way you're testing independent of the db call itself.
An example of what I mean in code, and sorry this is going to be a little rough as I'm less familiar with java compared to c#
say you have:
So, as stated above if your intention is to test the SQL itself, you should probably pull the literal SQL out, put it in a stored procedure, and use a unit testing framework to test the outcome of the proc under multiple scenarios like param value = 1, 2, and 3.
But, if you want to test the surrounding
// Do stuffand/or// potentially do some other stuff?without depending on database connectivity, you'll need to do some relatively simple refactoring.The method
myMethodThatNeedsTestinghas a dependency on the database, which we need to abstract away through the use of an interface, as to be able to test the methodmyMethodThatNeedsTestingwithout relying on a real database connection.That could look something like this:
I've defined the above to be a representation of what the query represents. The query requires a parameter (param1) and returns a scalar string (result from your query).
Given this interface, you can refactor your original class to look more like this:
In the above, you can see that the method
myMethodThatNeedsTestingis now no longer directly dependent on a database connection, but rather an interface. With this, we can now provide for testing purposes, a mock, stub, or fake.An example fake could be:
Now with the above fake, you pass in the fake implementation in your constructor, and you can test
myMethodThatNeedsTestingwithout relying on a database connection.The above refactor can be defined as dependency injection, and is quite useful for loose coupling, which leads to more easily tested code, among other things.
Sorry if i messed up any syntax in the above, again java is not my language of choice :)