0

I have a string variable in DAO interface and its implementation is used in junit.

I want to modify value of that variable before i invoke a method in DAO under JUnit.

For example the variable in interface has oracle native query and i want that be replace with a embedded database before i run the method which uses that variable.

I tried this but it doesn't work

Is there a way to do that?

Community
  • 1
  • 1
Prasad
  • 519
  • 9
  • 22
  • Post some code. But I don't see the point in testing a query that is not even the query that will be used in your production code. All your test will do is test a fake query. – JB Nizet Feb 19 '14 at 09:40

3 Answers3

0

Don't do this. Run your query against a real Oracle database instead.

By replacing the critical parts of your code you're not testing that it's going to work correctly in production. You get a +1 for using an embedded database but a -1 for replacing the actual query being sent.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • i cannot use that query for junit since it is production one. Also that query has case and decode in oracle which is not supported in hsqldb. Is there an alternate for this. – Prasad Feb 19 '14 at 12:43
  • Why can't you do it in JUnit? I'm suggesting running against a development database, not your production one. It might help to explain what it is you're trying to test – tddmonkey Feb 19 '14 at 14:35
  • Apart from our own oracle DB, We are using DB managed by external team through DB link with limited connections. Also accessing those DB is expensive and we can use it for JUnit. – Prasad Feb 19 '14 at 14:56
  • Ok leave about oracle query. I just want to know is there a way to change variable in interface? – Prasad Feb 19 '14 at 14:57
0

If the aim of your test is to verify the correctness of the query, you should run it against an Oracle DB instead of replacing the query string.

If the aim is simply about correct interaction between your DAO with DB Connection (or Hibernate Session, or JPA EntityManager etc), then you shouldn't replace the query. You should mock the DB Connection and verify if correct query is passed to DB connection for execution.

Neither of them needs you to replace your query string.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
-1

I guess you need to use some Mocking frameworks like EasyMock, PowerMock

Raju Rudru
  • 1,102
  • 12
  • 19
  • -1: you shouldn't be trying to *unit* test database access code; integration test against an actual database – tddmonkey Feb 19 '14 at 10:19