views:

380

answers:

5

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,

+2  A: 

Why don't you try Easymock or Mockito

Whiteship
+1  A: 

I use jUnit with jMock

pstanton
+6  A: 

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);
}
romaintaz
In my opionion that's a rather bad example; No annotations in 1.4.2 and horrible naming conventions.
Esko
That was just a copy paste from the blog post, but I will edit my answer to match the requirements.
romaintaz
Thanks for editing :)
Esko
That's my first experience seeing Mockito... very neat. Thanks!
Allain Lalonde
+3  A: 

There's a whole range of mocking libraries available for Java:

  • EasyMock, arguably the most popular mocking library at the moment. Wide range of features, easy to use.
  • Mockito, originally based on EasyMock's code, uses similar paradigms for mocking but automates several tasks such as switching mock object states (namely record, replay, verify, reset)
  • jMock, mocking based on Hamcrest Matchers. Haven't personally used this one but from what I've understood, it's at least decent.

...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.

Esko
Yea my company is a little bit behind the advancement of technology. I hope they will upgrade this application that I am maintaining soon.
zfranciscus
+1  A: 

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.

Steve Freeman
Well at the moment we are in the midst of maintaining an application that is not well written. Some of the classes have very big private methods that need serious refactoring. As part of this effort we are ttrying to make good unit test around the private methods and then slowly work our way up to refactor those methods
zfranciscus
I still don't think mocking private methods is a good idea, there are other technique. I'm assuming you've got Feathers' book on Working with Legacy Code?
Steve Freeman