首页 > 解决方案 > org.mockito.exceptions.misusing.InvalidUseOfMatchersException:编写单元测试时

问题描述

当我想测试我的方法时,我得到了这个错误,我是单元测试的新手。无法找到确切的问题。打印test6日志时,测试方法的倒数第二行似乎有错误。任何建议!

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.

这是我要测试的课程。

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;
    }

这是测试方法

  @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");
    }




标签: junitmockito

解决方案


推荐阅读