aspose file tools*
The moose likes Testing and the fly likes Returning an array of mock objects and testing their order using jmockit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Returning an array of mock objects and testing their order using jmockit" Watch "Returning an array of mock objects and testing their order using jmockit" New topic
Author

Returning an array of mock objects and testing their order using jmockit

Salil Surendran
Ranch Hand

Joined: Jan 16, 2004
Posts: 39
Hello Friends,
I am facing a particular problem where I have to test a method that reads a list of objects from a cache and then sorts them based upon priority. The list of objects as well as the cache has to be mock objects. However, once the method returns I have to test that these mock objects that are returned back by this method is sorted correctly based upon expected priority. Here is a very very simplified code sample which mimics what I am trying to do. In this code sample I have 3 Priority Beans which are mocked and I am trying to sort and return it back in ArrayComparator.sortBeans() method. However, the problems are obvious. The call to pb.getPriority() is not instance based but invocation count based and hence when the comparator calls getPriority on the bean for more than 3 times I get an UnexpectedInvocation error. Even I get through that in the assertEquals() method call pbArray[x].getPriority() method still calls the mock object. What I need are real stubs which are unique objects which can later on be compared. How do I do that in JMockit?

package com.salil.jmockit.mockarray;

import java.util.Arrays;
import java.util.Comparator;

public class ArrayComparator {

public PriorityBean[] sortBeans(PriorityBean[] beans){
Arrays.sort(beans,new Comparator<PriorityBean>() {

public int compare(PriorityBean o1, PriorityBean o2) {
return o2.getPriority() - o1.getPriority();
}
});
return beans;
}

}

package com.salil.jmockit.mockarray;

public class PriorityBean {

private int priority;

PriorityBean(int priority){
this.priority = priority;
}

public int getPriority(){
return priority;
}

}


package com.salil.jmockit.mockarray;

import static org.junit.Assert.assertEquals;
import mockit.Expectations;

import org.junit.Test;

public class MockArrayComparatorTest {

@Test
public void testSortBeans(final PriorityBean pb) {
new Expectations(){

{
pb.getPriority();
returns(5,10,0);
}
};

ArrayComparator ac = new ArrayComparator();
PriorityBean[] pbArray = ac.sortBeans(new PriorityBean[]{pb,pb,pb});
assertEquals(3,pbArray.length);
assertEquals(10,pbArray[0].getPriority());
assertEquals(5,pbArray[1].getPriority());
assertEquals(0,pbArray[2].getPriority());
}

}



Thanks,<br />Salil Surendran
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Why are you mocking out PriorityBean? It's just a POJO. I would create real PriorityBean objects and pass those to the method you are trying to test.

IN general, I only use mocks when the object I need to mock - hits the database, makes a remote call, does something complex, etc. I wouldn't mock out a java bean that just has getters/setters.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning an array of mock objects and testing their order using jmockit