首页 > 解决方案 > 添加和提交命令的单元测试

问题描述

我在我的代码中使用 JGit 库来添加和提交新文件。但我不确定如何通过 Mockito 编写单元测试。

这是使用 JGit 设置 repo 的模板。

private static final String PACKAGE_NAME = "ABC";
private static final String CLONED_REPO_PATH = String.format("/tmp/%s", PACKAGE_NAME);
final File gitFile = new File(String.join("/", CLONED_REPO_PATH, ".git"));
final Git openedRepo = Git.open(gitFile);

我想为这些特定语句编写一个单元测试:

  1. openedRepo.checkout().setCreateBranch(true).setName(branchName).call();
  2. openedRepo.add().addFilepattern(<some file>).call();
  3. openedRepo.commit().setMessage(commitMessage).call();

当我尝试使用此测试代码测试 [1](结帐分支步骤)时出现错误:
git.checkout().setCreateBranch(true).setName("test2").call();

Ref HEAD 无法解决

标签: javajunitmockitojgit

解决方案


推荐阅读