简体   繁体   English

在Jmockit Mocked对象中使用另一个对象作为参数

[英]Using another object as parameter in Jmockit Mocked object

I'm new to JMockit and have successfully run a basic unit test using it. 我是JMockit的新手,并且已经成功使用它运行了基本的单元测试。 However, I'm stuck when attempting to mock a Spring LdapTemplate. 但是,在尝试模拟Spring LdapTemplate时遇到问题。 The problem seems to be with the LdapQuery that is used by the LdapTemplate. 问题似乎出在LdapTemplate使用的LdapQuery上。 Do I need to mock this as well? 我也需要嘲笑吗?

JUnit Test JUnit测试

@RunWith(JMockit.class)
public class MyTest {

    @Mocked
    LdapTemplate mockLdapTemplate;

    @Test
    public void retrieveAccount_test() {

        Account acct = new Account();
        acct.setEmail("foobar@gmail.com");
        acct.setUserId("userA");
        final List<Account> expected = Arrays.asList(acct);

        new Expectations() {
            { 
              mockLdapTemplate.search(query().base(anyString).where(anyString)
                    .is("userA"), (AttributesMapper) any);
              result = expected;
            }
        };
        AccountService service = new AccountServiceImpl(mockLdapTemplate);
        Account account = service.retrieveAccount("userA");
        assertThat(account, is(notNullValue()));
    }
}

AccountService AccountService

public class AccountServiceImpl implements AccountService {

private LdapTemplate ldapTemplate;

@Autowired
public AccountServiceImpl(LdapTemplate ldapTemplate) {
    this.ldapTemplate = ldapTemplate;
}

@Override
public Account retrieveAccount(String userId) {
    LdapQuery query = query().base("ou=users").where("uid").is(userId);
    List<Account> list = ldapTemplate.search(query,
            new AccountMapper());
    if (list != null && !list.isEmpty()) {
        return list.get(0);
    }

    return null;
}

public class AccountMapper implements
        AttributesMapper<Account> {

    @Override
    public Account mapFromAttributes(Attributes attrs)
            throws NamingException {
        Account account = new Account();
        account.setEmail((String) attrs.get("mail").get());
        account.setUserId((String) attrs.get("uid").get());

        return account;
    }
}
}

(Leaving out the Account class since it should be self explanatory.) (省去了Account类,因为它应该是自解释的。)

If I replace mockLdapTemplate.search(query().base(anyString).where(anyString) .is("userA"), (AttributesMapper) any); 如果我替换了mockLdapTemplate.search(query().base(anyString).where(anyString) .is("userA"), (AttributesMapper) any); with mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any) the test passes (which is what I expect, but this more or less tells me the problem is with the LdapQuery parameter). 使用mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any)通过测试(这是我所期望的,但这或多或少告诉我问题出在LdapQuery参数上)。

Thanks! 谢谢!

You already know the answer: the expectation should be recorded as 您已经知道答案了:期望应该记录为

mockLdapTemplate.search((LdapQuery)withNotNull(), (AttributesMapper) any)

because this is the only mocked method being called from the unit under test. 因为这是被测单元唯一调用的模拟方法。 The argument matchers "any", "withNotNull()", etc. can only be used on calls to mocked methods, and LdapQuery was not mocked in the test. 的参数匹配器“任何”,“withNotNull()”等只能在调用方法嘲笑使用, LdapQuery没有在测试嘲笑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 期望中的模拟对象上的JMockit NullPointerException - JMockit NullPointerException on mocked object in Expectations 使用JMockit,具有相同模拟接口的2个对象返回的值相同 - Same value returned by 2 object with same Mocked Interfaces using JMockit 我可以将接口的jmockit模拟作为另一个对象的构造函数参数传递吗? - Can I pass a jmockit mock of an interface as a constructor parameter of another object? 如何使用@Transactional方法将jmockit的模拟对象注入弹簧管理对象? - How to inject mocked object by jmockit to a spring managed object with @Transactional method? 如何在JMockit模拟属性中设置@Before方法上的测试对象? - How to set in JMockit mocked properties to tested object on @Before method? jMockit:@Mocked mock object 未在非测试 class 中初始化 - jMockit: @Mocked mock object is not initialized in a non-test class JMockit - Expectations - 匹配包含模拟对象作为参数的方法的调用 - JMockit - Expectations - matching invocation of method that includes mocked object as argument EasyMock验证模拟对象的参数 - EasyMock verify parameter of mocked object 如何将模拟对象注入另一个已经模拟过的对象 - How to inject mocked object into another already mocked object 如何在 Java 中另一个模拟对象的模拟中注入模拟对象? - How to inject a mocked object inside mock of another mocked object in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM