英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论