Hi,
I am looking for a good unit test framework that I can use to mock private methods that can run under JDK 1.4.2.
Cheers,
Hi,
I am looking for a good unit test framework that I can use to mock private methods that can run under JDK 1.4.2.
Cheers,
Try Mockito and you will love it!
You can have a look of this library in this blog post, showing 6 simple examples of Mockito usage.
A short example:
@Test
public void iteratorWillReturnHelloWorld(){
//arrange
Iterator i = mock(Iterator.class);
when(i.next()).thenReturn("Hello").thenReturn("World");
//act
String result = i.next() + " " + i.next();
//assert
assertEquals("Hello World", result);
}
Edit regarding your requirements:
It seems that Mockito runs quite well on Java 1.4 and JUnit 3, as stated in this blog post.
The same example as above, but for Java 1.4:
public void testIteratorWillReturnHelloWorld(){
//arrange
Iterator i = Mockito.mock(Iterator.class);
Mockito.when(i.next()).thenReturn("Hello").thenReturn("World");
//act
String result = i.next() + " " + i.next();
//assert
assertEquals("Hello World", result);
}
There's a whole range of mocking libraries available for Java:
...and most likely some other less used libraries I haven't even heard of.
Since your requirement is JDK 1.4.2 support, that unfortunately means you can choose an old version of EasyMock or really old version of jMock. Even Java5's support is going to end in two days (30 October 2009, that is!) so if possible, try to get out from the 1.4.2 era - you (and/or your company) are simply far behind others and outside any kind of serious tech support.
No-one's picked this up, but why are you trying to mock private methods? It's almost always a bad idea since it breaks encapsulation.