0

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.

bm1729
  • 2,315
  • 3
  • 21
  • 30
  • Why didn't I think of that?! I guess because I don't have a ranking of 6655 :) . I think this is the best bet. Add this as an answer and I will mark it as correct. – bm1729 Nov 09 '12 at 15:02

2 Answers2

1

With EasyMock (v3.x) you are able to mock concrete class. See Readme

willome
  • 3,062
  • 19
  • 32
  • This doesn't seem to work for me (NoClassDefFoundError org/objenesis/ObjenesisHelper). I think I will go for Garrett's suggestion. Thank you anyway. – bm1729 Nov 09 '12 at 15:01
  • Some dependencies are missing. see http://stackoverflow.com/questions/8258797/how-configure-easymock-class-extension-3-1 – willome Nov 09 '12 at 15:13
1

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

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76