4

The expect doesn't seem to work for me:

    package com.jjs.caf.library.client.drafting;

import static org.junit.Assert.*;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import com.jjs.caf.library.client.CustomerManager;
import com.jjs.caf.library.client.UserBookLimiter;

public class DraftTest {

    UserBookLimiter userBookLimiter;
    int expected = 5;

    @Before
    public void setUp() throws Exception {
        userBookLimiter = EasyMock.createMock(UserBookLimiter.class);
        EasyMock.expect(userBookLimiter.getMaxNumberOfBooksAllowed()).andReturn(5);
    }

    @Test
    public final void test() {
        assertEquals(expected, userBookLimiter.getMaxNumberOfBooksAllowed());
    }

}

It's supposed to be 5, but I'm getting 0 as if the expect wouldn't be there at all...

Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
Arturas M
  • 4,120
  • 18
  • 50
  • 80

2 Answers2

15

You need to call the replay method on your mock object, so that it starts returning what you configured it to.

kgautron
  • 7,915
  • 9
  • 39
  • 60
  • 1
    As an additional note: if DraftTest is a class instead of an interface, you have to use org.easymock.classextension.EasyMock.* – Pieter De Bie Mar 17 '16 at 12:40
14

Okay, after analysing I finally got it to work by adding EasyMock.replay(userBookLimiter);

So the setup method looks like this:

@Before
public void setUp() throws Exception {
    userBookLimiter = EasyMock.createMock(UserBookLimiter.class);
    EasyMock.expect(userBookLimiter.getMaxNumberOfBooksAllowed()).andReturn(5);
    EasyMock.replay(userBookLimiter);
}
Clijsters
  • 4,031
  • 1
  • 27
  • 37
Arturas M
  • 4,120
  • 18
  • 50
  • 80