Search code examples
javaeasymock

Mock an argument that expects a value fails in EasyMock


I have a class which implements an interface and a method which takes another interface. I wonder to mock method calls from that argument; however, the result is null.

public class LoginAction implements IServerAction {
private static final int HTTP_OK_STATUS = 200;

    @Override
    public void execute(IServerExchange exchange) throws Exception {
        URI uri = exchange.getRequestURI();
        exchange.setStatus(HTTP_OK_STATUS);
        GeneralHelper.encrypt('some text');
}

here is my test:

@PrepareForTest({GeneralHelper.class})
@RunWith(PowerMockRunner.class)
public class LoginActionTest {
 @Before
    public void setUp() throws Exception {
        loginAction = new LoginAction();
        iServerExchange = createMock(IServerExchange.class);
}
 @Test
    public void testExecute_userExistsInScoreList_success() throws Exception {
       expect(iServerExchange.getRequestURI()).andReturn(new URI("/254/login"));
       expect(GeneralHelper.encrypt('some text').andReturn('my test');
       loginAction.execute(iServerExchange);
}

The uri is null in the test. Also the GeneralHelper which is a final class is not mocked entirely as i expect to return a value.


Solution

  • I should have considered replay method before calling the main method.