java - How do I replace a certain class when running some JUnit tests? -
java - How do I replace a certain class when running some JUnit tests? -
i'm using java 6 junit 4.8.1 (and maven 3.0.3). junit tests, want replace class of tests dependent on (qualified com.myco.clearing.product.server.cache) own version of class (which has same public method signatures). elegant way this?
note of junit tests don't invoke com.myco.clearing.product.server.cache.cache class directly, rather phone call classes rely on class. in these indirect cases, want version of class used.
thanks help along these lines, - dave
it hard exactly here, in general:
use ioc techniques - contructor, setter injection.
use mocking framework mock objects, recommend mockito.
so, example, if have class uses com.myco.clearing.product.server.cache.cache
let's phone call foo
. , utilize this:
class foo { private cache _cache = new cache( ... ); }
if possible, alter this:
class foo { private cache _cache; public foo( cache cache ) { _cache = cache; } public foo() { this( new cache() ); } }
then may able inject in mocked version of cache
class. "may able to" depends on cache
class. if final, has bunch of static methods, etc. have more deal with.
java junit mocking
Comments
Post a Comment