0

When I want to test my method I got this error, I am new to unit testing. Not able to find the exact issue.Seems like error in the second last line of the test method while printing test6 log. Any suggestion !

12 Jul 2021 13:51:54,915 [INFO ]  (main) amazon.platform.config.AppConfigTree:1756: No appgroup found
test1
test2
test3
test4
test5

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
4 matchers expected, 3 recorded:

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

This is the class that I want to test.

public String addCommitAndPushUntrackedChanges(final String deviceName, final List<String> filesList)
            throws GitAPIException, IOException, DependencyFailureException {
        final String tempCommitBranchName = String.format("Add Sync "
                + "rules : [%s]", deviceName);
        final File gitFile = new File(String.join("/", CLONED_REPO_PATH, ".git"));
        String CommitId = jgitAccessor.addAndCommitUntrackedChanges(gitFile, deviceName, tempCommitBranchName, filesList);
        gitFarmServiceAccessor.loadTmpSshTicket();
        jgitAccessor.pushToTemporaryBranch(gitFile, REPO_URI, deviceName);
        return CommitId;
    }

This is the test method

  @InjectMocks
    CreateIngestionRulesTaskBuilder builder;

@Test
    public void addCommitAndPushUntrackedChanges_happyCase() throws Exception {
        System.out.println("test1");
        Mockito.doNothing().when(gitFarmServiceAccessor).loadTmpSshTicket();
        System.out.println("test2");
        System.out.println("test3");
        Mockito.doNothing().when(jgitAccessor).pushToTemporaryBranch(REPO_FILE, REPO_LOCATION,"abc");
        System.out.println("test4");
        when(jgitAccessor.addAndCommitUntrackedChanges(eq(REPO_FILE),eq(TEST_DEVICE_NAME),eq(TEST_COMMIT_MSG), anyList())).thenReturn(eq(TEST_COMMIT_ID));
        System.out.println("test5");
        builder.addCommitAndPushUntrackedChanges(any(), anyList());
        System.out.println("test6");
    }




Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • 1
    This is not the reason for error, but in the return should we return TEST_COMMIT_ID, in place of eq(TEST_COMMIT_ID) – Deepak Patankar Jul 12 '21 at 08:47
  • 1
    Second error: you call your method under test with matchers as arguments, instead of passing in real values. – Lesiak Jul 12 '21 at 08:49
  • thanks @Lesiak , So you want to tell that instead of writing `builder.addCommitAndPushUntrackedChanges(any(), anyList());` we need it to replace with `builder.addCommitAndPushUntrackedChanges(TEST_DEVICE_NAME, anyList())` and what about the second argument? – SATYAM MITTAL 17103094 Jul 12 '21 at 09:38
  • Similarly to first argument - you need to pass a list, not a matcher. – Lesiak Jul 12 '21 at 09:57
  • Thanks @Lesiak, it works ! But One query , Why we can not pass a matcher like in this case ? does Mockito is not able to autofill the values a/c to the requirement? – SATYAM MITTAL 17103094 Jul 12 '21 at 10:15
  • Mockito matcher methods return dummy values (empty list in case of anyList()), but their inner working is a bit intricate and they cannot be used outside of stubbing context. For primers, see https://stackoverflow.com/questions/22822512/how-do-mockito-matchers-work – Lesiak Jul 12 '21 at 10:51

0 Answers0