4

I am working on a Java web app with unit/integration tests. App gets deployed to Jetty and uses H2 db while running the integration test phase of maven. I've one oracle function which is called from dao layer which can not be migrated to H2 db, hence i want to mock/skip this part in code while running the test cases.

I thought of having a flag which can tell if i'm running application in test mode and put the condition in code for it, but doesn't look like cleaner approach to me. Please suggest best approach to achieve this.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Pankaj
  • 3,512
  • 16
  • 49
  • 83

3 Answers3

3

Extract the Oracle native call into a separate class (probably some DAO). Inject that DAO to class that uses it. Create a second implementation of that DAO, doing nothing in place of calling Oracle. During integration testing inject the latter implementation.

Avoid flags in your code. If you are using Spring, use build profiles that will selectively create one implementation or the other.

That's how dependency injection helps you test your code: if you want to mock some part of the system, just inject mocked version.

alexbt
  • 16,415
  • 6
  • 78
  • 87
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • excellent! it works for me. I used of the spring 3.1 to inject the dependency based on environment in which i'm running the application. However other solutions are also good depending on scenario. – Pankaj Oct 05 '12 at 20:21
  • @Pankaj: so you did it exactly the way I would do this as well. Glad I could help. – Tomasz Nurkiewicz Oct 05 '12 at 20:24
1

Please use some good Mocking frameworks such as mockito or jMock or some other similar mock frameworks.

Please Note: You might be required to re-factor your code to make it more testable.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

If the question truly is:

How do I skip a section of code when unittesting in java

then I agree with the answers given. Dependency injection, mocking frameworks are absolutely the right way to go to do true unit testing.

However if the question is:

How do I skip a section of code when using JUnit (or other unit testing framework)

Then I think the answer is "it depends". Sometimes I use JUnit for integration testing - snippets of client code that I run against a test server to save me the trouble of doing these client side tests manually via a GUI. In this case I use system properties for example in my base class I have:

protected boolean skipTest()
{
    String port = System.getProperty("jersey.test.port");
    // don't run this test unless developer has explicitly set the testing properties
    // this is an integration test, not a unit test
    return port == null;
}

Then in the actual test class it looks like this:

// verify a successful login
@Test
public void testLogin()
{
    if (skipTest())
       return;

    // do real test

So, my thought is if you really cannot refactor the Oracle stuff out of your DAO, then you really are doing an integration test and it's OK to have a skipTest in your unit test.

Guido Simone
  • 7,912
  • 2
  • 19
  • 21