简体   繁体   English

如何在 Java 中模拟 LdapTemplate 并获得完整的代码覆盖率

[英]How to Mock LdapTemplate in Java and get full code coverage

I am trying to get full coverage on a very simple junit test using Mockito and am striking out.我正在尝试使用 Mockito 对一个非常简单的 junit 测试进行全面覆盖,并且正在脱颖而出。 My disclaimer is that I am new to Mockito though what I am trying to do would seem pretty straightforward.我的免责声明是我是 Mockito 的新手,尽管我想做的事情看起来很简单。

Note that my junit test passes, it is just that the coverage is not complete.请注意,我的 junit 测试通过,只是覆盖不完整。 When the test is run, the part of the method that returns true (users list is not empty) is not getting run/covered.运行测试时,返回 true 的方法部分(用户列表不为空)未运行/覆盖。

My questions are...我的问题是...

  1. Does ldap need to get primed with any test data? ldap 是否需要准备好任何测试数据?

  2. Can you simply mock ldap data?你能简单地模拟 ldap 数据吗?

Any insight you can offer is greatly appreciated.非常感谢您提供的任何见解。

Here is the method (class name = LdapRepository) being tested...这是正在测试的方法(类名 = LdapRepository)...

public Mono<Boolean> ldapTemplateQuery(Email email) {

        Mono<Boolean> blockingWrapper = Mono.fromCallable(() -> {
            List<LdapUser> users = ldapTemplate.find(query().where("cn").is(email.address()), LdapUser.class);
            if (users.isEmpty()) {
                return false;
            } else {
                return true;
            }
        }).onErrorResume(
                original -> Mono.error(new UserNotFoundException("User not found for email " + email.address())));
        return blockingWrapper.subscribeOn(Schedulers.elastic());
    } 

And here is the junit class...这是 junit class ...

@RunWith(MockitoJUnitRunner.class)
public class LdapUserRepositoryMockTest {

    private @InjectMocks LdapUserRepository ldapUserRepository;

    @Mock
    private LdapTemplate ldapTemplate;

    Email email = new Email("abcd@xyz.com");
    User user = new User(email);

    static final String password = "VGVzdEAxMjM=";
    
    @Test
    public void ldapTemplateQueryTest() {

        LdapUser ldapUser = new LdapUser(user, password.toCharArray());
        List<LdapUser> users = new ArrayList<>();
        users.add(ldapUser);

        lenient().when(ldapTemplate.find(query().where(Mockito.anyString()).is(Mockito.anyString()), LdapUser.class)).thenReturn(users);
        
        Mono<Boolean> locked = ldapUserRepository.ldapTemplateQuery(email);
        StepVerifier.create(locked).expectNext(false).verifyComplete();
    }

And here is the screenshot of the coverage results...这是覆盖结果的屏幕截图......

在此处输入图像描述

With much help from someone, I was able to get my answer.在某人的大力帮助下,我得到了答案。 As you will see below, it was a rather minor change to the lenient statement.正如您将在下面看到的,这是对宽松声明的一个相当小的改动。

Hopefully this will be a help to someone along the way.希望这对沿途的人有所帮助。

@RunWith(MockitoJUnitRunner.class)
public class LdapUserRepositoryMockTest {

    private @InjectMocks LdapUserRepository ldapUserRepository;

    @Mock
    private LdapTemplate ldapTemplateMock;

    Email email = new Email("abcd@xyz.com");
    User user = new User(email);

    static final String PASSWORD = "VGVzdEAxMjM=";

    @Test
    public void ldapTemplateQueryTest() {

        LdapUser ldapUser = new LdapUser(user, PASSWORD.toCharArray());
        List<LdapUser> users = new ArrayList<>();
        users.add(ldapUser);
 
        lenient()
                .when(ldapTemplateMock.find(Mockito.any(), Mockito.eq(LdapUser.class)))
                .thenReturn(users);

        Mono<Boolean> locked = ldapUserRepository.checkIfAccountLocked(email);
        StepVerifier.create(locked).expectNext(true).verifyComplete();
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM