Kidambi Manoj
Kidambi Manoj

Reputation: 131

Exclude class from a dependency

I want to switch between dependencies in the runtime.

Pseudo code:

//use com.realdependency.ConnectionFactoryImpl; 
    Class A {
           // intergration test  
       void testWithLiveDomain() {
          Connection connection = ConnectionFactory.getInstance().getConnection(domain);
       }

    }

// use com.mymockeddependency.ConnectionFactoryImpl; 
class B  {
   // unit test
   void testWithMockDomain() {
      Connection connection = ConnectionFactory.getInstance().getConnection(domain);
   }
}

If I have both the dependencies in my POM.xml, my class B is using the real dependency instead of the mocked one. How do I restrict class B to use the mocked dependency

Upvotes: 0

Views: 244

Answers (1)

dreamsComeTrue
dreamsComeTrue

Reputation: 116

This is the perfect candidate for Dependency Injection. If this is is web-app or you can add additional libs like Spring or any kind of DI container (PicoContainer?) you can leverage them to pick dependencies at runtime.

Try googling "java @Alternative annotation".

Upvotes: 1

Related Questions