2

I have a Java application that talks to a MySQL database using Spring JDBC Templates. In one of my tests I want to simulate a table not being there in an integration test, however I cannot actually temporarily drop/recreate or rename the table in our testing database for a variety of reasons.

I also don't want to mock out the particular DAO method call that would hit the table that throws the error in case going forward we use other methods. I want to do something like intercept the sql before it goes off to the database and replace the table name (if present) with a bogus name--or something else to the same effect.

Test driven development for the win!

2
  • Is this a unit test? If so, if it fails, do you care? Commented Apr 20, 2012 at 15:43
  • Simple. Excecute this test case before the DBA creates the tables. Commented Apr 20, 2012 at 15:45

2 Answers 2

1

If interception is what you want, using aspects could be a solution.

  • You'd have to create a pointcut intercepting, e.g., the call to the specific method "sending the sql before it goes off to the database".

  • Then you can create before/around advice, in which you can manipulate the data ("replace the table name with a bogus name") before you proceed with the call

See , as well as Aspect Oriented Programming with Spring.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use EasyMock for tests like these. There are ways to have a method throw any exception you specify. If you cannot use this, you have not effectively separated your application layers.

Look at: http://www.easymock.org/EasyMock2_2_Documentation.html, under the heading "Working with Exceptions"

3 Comments

Usually we use easyMock but since the problem is a non-existant table there could be many data access layer functions that would throw exceptions--some perhaps not even written yet.
I've never heard of testing for functions that don't even exist yet! This points to that you don't have your data and service layer abstracted enough.
Sorry if I didn't make sense :( Basically if I use one function to interact with that table I could mock it to throw an error. But if somebody makes a new function that depends on that table unless they remember to also mock it to throw an error to test that table not existing we could miss some things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.