在EasyMock中模拟期望值的参数失败

问题描述

我有一个实现接口的类和一个采用另一个接口的方法。我想从该参数模拟方法调用;但是,结果为空。

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');
}

这是我的考试:

@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);
}

在测试中uri为空。另外,作为最终类的GeneralHelper并没有完全被嘲笑,因为我希望返回一个值。

解决方法

在调用main方法之前,我应该已经考虑过重播方法。