简体   繁体   中英

Is the object I'm mocking what I think it is? Wanted but not invoked error

I have a setter, that sets the hash value of a certain Document object. When I add/set properties Document (text, publicationDate, author) It should go into each of the if statements and complete the stringbuilder. I want to check if the update method was called, and was called with the "concatenated strings". In this case, i'm not sure what should be called with update() , hence I put an empty "" string.

I'm expecting the verify to look like this, verify(....).update(<concantenateDocStuff>.getBytes())

public void setHash() {
        StringBuilder sb = new StringBuilder();

        if (text != null) {
            sb.append(text);
        }
        if (publicationDate != null) {
            sb.append(publicationDate.toString());
        }
        if (authors != null) {
            sb.append(
                String.join(
                    "",
                    authors.stream()
                        .map(a -> a.getAuthor())
                        .collect(Collectors.toList())
                )
            );
        }
        try {
            MessageDigest msgDigest = MessageDigest.getInstance("md5");
            msgDigest.update(sb.toString().getBytes());

            hash = Hex.encodeHexString(msgDigest.digest());
        } catch (NoSuchAlgorithmException ex) {
            LOG.error(ex.getMessage(), ex);
        }
    }
@Test
public void testSetHash() throws Exception {
        String hash = "hash";
        DateTime date = PowerMockito.mock(DateTime.class);
        Author author = Mockito.mock(Author.class);
        MessageDigest msgDigest = Mockito.mock(MessageDigest.class);
        PowerMockito.spy(MessageDigest.class);
        PowerMockito
            .doReturn(msgDigest)
            .when(MessageDigest.class)
            .getInstance("md5");
        Mockito
            .doNothing()
            .when(msgDigest)
            .update(Mockito.any(byte[].class));

        byte[] byteArray = new byte[1];
        Mockito
            .doReturn(byteArray)
            .when(msgDigest)
            .digest();

        Document testDocument = new Document();
        testDocument.setText("text");
        testDocument.setPublicationDate(date);
        testDocument.addAuthor(author);

        testDocument.setHash();

        Mockito
            .verify(msgDigest, Mockito.times(1))
            .update("".getBytes());
    }

Am I mocking the correct object?

Wanted but not invoked:
messageDigest.update([]);

Refactor your method setHash() to make it return String(change from set to get). And make hash = getHash(); I would say that Unit Test in this case is a bad idea. Cause all you gonna test is your mocks and StringBuilder behavior. Just make few integration tests:

1.Was returned String with all components(text, publicationDate, authors)
2.Was returned empty String (without all components)
3.Was thrown exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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