2

Is there any framework, whick allows to mock concrete classes, not only interfaces in java 1.4? I have third party code with a singleton class, where I wanna change one function, without touching orignal code. Is it possible?

2 Answers 2

2

JMock 1.2 supports this. Details here.

4
  • Can I use JMock without unit tests? As I see I need to extend class MockObjectTestCase? In my situation it would be very unhandy to use unit tests...
    – Filip
    May 31, 2010 at 14:00
  • You don't need to use a unit test, but certain methods will need to be run. I haven't tried it, but you can check the source code of MockObjectTestCase and see if it does anything special that has to be called separately.
    – Yishai
    May 31, 2010 at 14:09
  • Is it possible to mock some class so all instantiation of some class created after some moment are mocked? I know it is possible in jmockit, but it's only available for newer java.
    – Filip
    Jun 1, 2010 at 8:48
  • @Filip, I don't think JMock naitively supports such a concept. Of course you could fake it by having a factory produce the object with that logic in it. AFAIK such a feature requires classloader intervention, which would make it tricky to run in a production system.
    – Yishai
    Jun 1, 2010 at 13:06
2

I recommend Mockito. A port to Java 1.4 is available here.

2
  • I'm trying to use Mockito with Java 1.4 and ran into a problem almost immediately: MyClass myClass = (MyClass) mock(MyClass.class); when(myClass.returnsInt()).thenReturn(???); thenReturn returns only an Object. Not a problem with auto boxing/unboxing in Java 1.5, but I don't know how to get around this problem in Java 1.4.
    – iboisver
    Feb 23, 2012 at 0:17
  • Scratch that. If I write when(myClass.returnsInt()).thenReturn(new Integer(1)); it looks like Mockito converts the Integer to a primitive int on a call to returnsInt() and everything "just works".
    – iboisver
    Feb 23, 2012 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.