Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use EasyMock expect

Tags:

java

easymock

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...

like image 597
Arturas M Avatar asked Nov 28 '22 09:11

Arturas M


2 Answers

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

like image 178
kgautron Avatar answered Dec 05 '22 17:12

kgautron


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);
}
like image 20
Arturas M Avatar answered Dec 05 '22 16:12

Arturas M