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

huangapple go评论74阅读模式
英文:

Mock an argument that expects a value fails in EasyMock

问题

我有一个实现了一个接口的类以及一个接受另一个接口作为参数的方法我想要模拟对该参数的方法调用然而结果是 null
```java
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);
}
英文:

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.

答案1

得分: 0

我在调用主方法之前应该考虑重放方法。

英文:

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

huangapple
  • 本文由 发表于 2020年10月12日 12:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64311847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定