views:

314

answers:

3

Hi,

I'm using the Mockito library for Java testing and getting errors in Mockito when I run my test. (I'm using the NetBeans IDE, in case that's relevant). For example, I have a class called Animal and I am trying to perform the following simple test:

@Test
public void mokito_test(){

    Animal mockAnimal = mock(Animal.class);
    Animal testAnimal2 = mockAnimal;

    assertTrue(mockAnimal.equals(testAnimal2));

}

This test gives the following error:

mokito_test caused an ERROR (at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:37))
  at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:37)
  at org.mockito.internal.util.CreationValidator.validateType(CreationValidator.java:14)
  at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
...etc.

(There are 11 more errors, including in java.net, java.security, java.lang, and sun.misc.)

However, if I perform this test using a real object instead of a mocked object, the test is successfull:

@Test
public void animal_test(){

    Animal testAnimal1 = new Animal("bear");
    Animal testAnimal2 = new Animal("bear");

    assertTrue(testAnimal1.equals(testAnimal2));

}

This test is successfull.

I have downloaded the Mockito jar file (mockito-core-1.8.0.jar) into my project directory, and then referencing the relative path of the jar file in the testing library for this particular project. I've never used Mockito before, so I suspect that my error has something to do with my system configuration. Any help would be greatly appreciated! Thanks!

+2  A: 

Mockito has some external dependencies on objenesis and hamcrest libraryes, line 37 attempts to construct an object from objensis library.

Please use mockito-all-1.8.jar instead.

notnoop
Thanks for the answer, I will try to implement this.
physicskate
A: 

Are you using mockito with junit test.. was not sure what you were trying to test.. After adding the required jar file (mockito-all jar), create a test like this. since you are using annotation you can use @Mock to create a mock object

    @RunWith(MockitoJUnitRunner.class)
    public class AnimalTest {

    @Mock
    private Animal mockAnimal;

    @Test
    public void mokito_test(){

        when(mockAnimal.toString()).thenReturn("Some String");

        String toStringResult = mockAnimal.toString();

        //verify(mockAnimal).toString();  -- to verify toString() method called once on the mock
        assertTrue("Some String".equals(toStringResult);

    }
}

If your Animal class use Object C method b then you mock the method of the object C like shown above.

surajz
A: 

The test you are running is not testing anything, nor is it trying to verify any interactions. Mocks are used in object orientated systems to specify how objects interact with each other - so to check that one object tells other objects to do things.

They shouldn't be used to test calculations, or state changes in value objects. Using mocks in these types of tests lead to brittle tests, because you are just duplicating the underlining implementation of your method.

So methods on value objects - such as equals, hashCode, toString, should always be tested using the real object.

This process is described very well in a book called "Growing Object-Oriented Software, Guided by Tests".

rhys keepence
yup I know. Read the last line of my comment. I mocked toString() method because I did not know what methods were available on Animal class. I should have probably mocked speak() method or better Vet.address method.
surajz