1

I'm new at unit testing and am running into an issue with jMock that I can't seem to figure out. I have a public final instance variable which I need to define an expectation for, but I can't get it to work. If I make a getter for the variable, it works, but I'd rather not have to create a bunch of getters just to make unit testing work. Any help on how to do this would be much appreciated. Here's some code illustraiting what I'm trying to do:

public class Main {
    private SimpleObject simpleObject;

    public Main(SimpleObject o){
        this.simpleObject = o;
    }

    public int iDontWork(){
        return simpleObject.myList.size();
    }

    public int iWork(){
        return simpleObject.getMyList().size();
    }
}

My test:

@RunWith(JMock.class)
public class MainTest {
    Mockery context = new Mockery() {{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};
    @Mock
    SimpleObject simpleObject;
    private Main main;

    @Before
    public void setup(){
        main = new Main(simpleObject);
    }

    @Test
    public void itWorks() {
        context.checking(new Expectations() {{
            oneOf(simpleObject).getMyList(); 
            will(returnValue(new ArrayList<String>(Arrays.asList("Hey"))));
        }});
        int i = main.iWork();
        assertEquals(1, i);
    }

    @Test
    public void itDoesntWork() {
        context.checking(new Expectations() {{
            oneOf(simpleObject).myList.size(); will(returnValue(1));
        }});
        int i = main.iDontWork();
        assertEquals(1, i);
    }
}

SimpleObject:

public class SimpleObject {
    public final List<String> myList;

    public SimpleObject(){
        myList = Collections.unmodifiableList(Arrays.asList("Hey"));
    }

    public List<String> getMyList(){
        return myList;
    }
}
7
  • Um, where's that public final instance variable you're talking about? I don't see one anywhere. Additionally, it's conventional to create the getters anyway, because lots of automated tooling scans getters to find out what fields an object has. Perhaps look at Groovy, which generates them for you automatically? Jan 18, 2015 at 3:14
  • Ah, I think you're talking about on SimpleObject. It's convention to have those getters. Jan 18, 2015 at 3:16
  • Really? Some open source projects disagree: "Prefer public final fields to private fields with getters." Jan 18, 2015 at 3:41
  • I disagree with the Cassandra team, then. Not having the getter will prevent JavaBeans-based tooling from automatically handling the property. (That said, I'm usually going to use Groovy and not have the boilerplate anyway.) Jan 18, 2015 at 3:57
  • Why do you want to mock out the list? It's completely encapsulated by SimpleObject, so you can just test the behavior of SimpleObject via it's public API. It's extremely rare to need to mock collections classes because their behavior is vast and a function of the data in the collection, so there are no benefits to a mock (and real costs, because you might be making specifications on the collection that aren't realistic) Jan 18, 2015 at 4:15

1 Answer 1

0

A mock object implements methods of the real object. It does not have fields of the real object (even if these fields are public).

1
  • A mock object implements some part of the public API of the real object. Public fields are part of that public API. Jan 18, 2015 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.