I want to test effect of network connection drop exactly before the execution of specific line of a program. Such as consider this execution sequence:
1. Connect to database.
2. Get country list from database.
3. Get a random country from list.
4. Get city list of this country from database.
I want to test behavior of program if network connection drops just before statement 4. I’m able test it with debugger by putting a break point, closing database when it hits break point, and then continuing to run program.
I wonder how can I accomplish this in a more systematic and robust way.
Let’s make the assumption that the 4 lines of pseudo code are in MyService, and that MyService uses MyDAO to access the database, you would have the following:
To test it, use a mocking framework like Mockito to mock MyDAO, and inject this mock into a MyService instance. Make the mock throw the same exception as the one thrown by a real MyDAO when the network goes down when its
getCities()method is thrown, and see is MyService does the right thing: