Search code examples
javatestingmockingjunit3

Mocking a concrete class in junit 3


I have a class which has the following constructor:

class Foo {
    Foo (Bar bar) {
       ...
    }
}

I am attempting to write a unit test for this class and Mock out the dependency on Bar. However, I have to use JUnit 3 and Bar is a concrete type. Does anyone have any ideas? I cannot use EasyMock class-extension (requires JUnit 4) and haven't had success with Mockito. One (particularly ugly) solution I am considering is as follows:

interface IBarWrapper {
    void barMethod();
}

class BarWrapper implements IBarWrapper {
    void barMethod() {
        bar.barMethod();
    }
}

class Foo {
    Foo (IBarWrapper wrapper) {
        ...
    }
}

But I don't like the idea of changing my actual code to suit the tests.


Solution

  • You can just subclass Bar. No need to over complicate things.