2

I am experiencing unexpected behavior of EasyMock when writing junit test. Finally, I deleted some lines of code until there is one line of creating mock left to see what happened.

@Test
public void testSimple() throws Exception {
    Socket socket = EasyMock.createMock(Socket.class);
}

This junit test generates error:

[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.052 sec

This is not the case when I just have normal instantiation like:

@Test
public void testSimple() throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
}

This junit test seems happy with that:

[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.047 sec

I wonder if someone can clear this ambiguity. Thanks.

knd
  • 1,662
  • 2
  • 15
  • 27

3 Answers3

4

You're attempting to make a mock of a concrete class (Socket is not an interface), so you probably need to do a little extra work.

Assuming you are using 3.X version of EasyMock, you likely need to add the other libraries required for class mocking:

cglib (2.2) and Objenesis (1.2) must be in the classpath to perform class mocking

Gray
  • 115,027
  • 24
  • 293
  • 354
victorvess
  • 41
  • 1
  • As of EasyMock 3.0 (3.4 now) you don't need to specify `cglib` or `Objenesis`. Mocking classes just works. – Gray Apr 07 '17 at 03:31
3

If you are mocking concrete classes you need to use EasyMock Class Extension

http://easymock.org/EasyMock2_2_2_ClassExtension_Documentation.html

Link shows how. http://helpdesk.objects.com.au/java/how-do-i-mock-a-concrete-class-using-easymock

smk
  • 5,340
  • 5
  • 27
  • 41
2

As of EasyMock 3.0 (the current version is 3.2), you can mock concrete classes so you would not get this error now.

Zoltán
  • 21,321
  • 14
  • 93
  • 134