【问题标题】:Mockery - Unexpected Expectations - Not sure what I am doing wrong嘲弄 - 意想不到的期望 - 不知道我做错了什么
【发布时间】:2017-03-22 07:25:26
【问题描述】:

我有一个服务界面如下:

public interface accountsService {

    public accountRemovalModel purgeAccounts();
}

我必须关注测试类:

我有一个服务界面如下:

public interface AccountsService {

    public accountRemovalModel purgeAccounts();
}

我必须关注测试类:

public class AccountsServiceTest extends BaseTestClass {

    private Mockery _m = new Mockery() {
    {
        setImposteriser(ClassImposteriser.INSTANCE);
    }};


    private accountsService _accountsService;

    @Before
    public void beforeTests() throws Exception {

        accountsService = _m.mock(AccountsService.class);
    }

    @Test
    public void testNoItemsToDeleteSuccess() throws Exception {

        // Return a simple AccountsRemovalModel
        // APPARENTLY THIS EXPECTATION IS UNEXPECTED?
        _m.checking(new Expectations() {{

            allowing(accountsService.purgeAccounts());
            will(returnValue(new accountRemovalModel(0,0)));
        }});

        accountsRemovalModel result = accountsService.purgeAccounts();

        Assert.assertEquals(0, result.getDeleteCount());
        Assert.assertEquals(0, result.getTotalCount());
    }
}

我收到以下错误:

AccountsServiceTest.testNoItemsToDeleteSuccess:23 » 预期意外...

非常感谢您对此的任何帮助 - 因为我在让它工作时遇到了很大的问题!

【问题讨论】:

  • 你不使用mockito,而是使用jmockit。我更新了标签
  • 你是对的,那是我有一个白痴时刻-我们通常使用mockito-在这个项目中我们使用jmockit。
  • 对不起,JMock :) 对不起,我不知道,但您似乎在模拟被测方法的方法。你不应该。

标签: java unit-testing junit junit4 jmock


【解决方案1】:

您的语法稍有偏差 - 您需要在被模拟的对象周围加上括号 (accountsService):

    _m.checking(new Expectations() {{

        allowing(accountsService).purgeAccounts();
        will(returnValue(new accountRemovalModel(0,0)));
    }});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多