Search code examples
testingmockingtypemockisolation

Isolating Dependencies Without Inversion Of Control


I'm working on an enterprise application that relies heavily on message queues, com+, databases, httpcontext, static utility classes etc.

Since there's 600 projects in our system it seems impractical to rewrite to use inversion of control. Typemock claims they are the only isolation framework that doesn't require you to rewrite your code to use IOC.

Does anybody know how TypeMock implemented this level of functionality and if there are any alternatives? Even if I was to rewrite my application to use inversion of control I would have to write wrapper classes for message queues, httpcontext etc. To me that just sounds ridiculous, am I right or wrong to think that Typemock is the only viable option for my scenario.

Thanks


Solution

  • You are right to think that TypeMock (or another similar mocking tool) is the only viable option.

    It's always possible to use an AOP tool directly to provide isolation of dependencies, but the effort required to do so is significant, making it not viable in practice.

    For Java, the JMockit toolkit enables isolation for all kinds of dependencies, without any necessary changes to production code.

    Internally, JMockit uses the functionalities provided by the java.lang.instrument API. Basically, it allows methods/constructors to be redefined at runtime. Redefinition means the bytecode implementing the method/constructor is replaced. This can be done any number of times for the same method. Additionally, classes can be transformed at load time (i.e, any method or constructor defined in the class can have its bytecode changed just before the class is made available to the JVM).