Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
所以我要寻找一个库或某种方式来拦截Java应用程序发出的所有HTTP请求?为什么?因为我想对API集成进行单元测试,并且因为我正在使用该API的库(包装器),所以我无法修改其任何代码。实际发出HTTP请求的不是我的代码。

因此,根据API的文档,我需要拦截它们,查看它们并断言它们是正确的。

我尝试过在线查找,但是找不到确切的内容。仅当我自行正确配置请求时,大多数库才允许我执行此操作,但我不能这样做,因为它们是由API包装而不是我的代码创建的。

干杯

P.S一些示例代码

String path = "/some/path";
String repoOwner = "John Doe";
String repoName = "John repo"

GitHubClient client = new GitHubClient();
client.setCredentials(this.username, this.password);

RepositoryService repositoryService = new RepositoryService(client);
CommitService commitService = new CommitService(client);

Repository repo = repositoryService.getRepository(repoOwner, repoName);

List<RepositoryCommit> commits = commitService.getCommits(repo, null, path);


这将使用API​​包装器来获取给定存储库中的所有提交。假设我想查看此代码发出的HTTP请求,然后想对其进行单元测试并断言它们是正确的。我该怎么办?有什么方法可以拦截它们,像捕获异常一样捕获它们,并对其进行处理,例如断言?

最佳答案

您要尝试执行的操作并不容易,从我的角度来看,这没有多大意义。

您正在尝试测试您的应用程序,但是大多数情况下,您正在尝试测试GitHubClient库。

如果我是你,我只会在测试中使用模拟并验证输入参数。因此,测试需要这样(使用mockito):

List<RepositoryCommit> commits = new ArrayList<>();
commits.add(new RepositoryCommit(....));
commits.add(new RepositoryCommit(....));
when(commitService.getCommits(repo, null, path)).thenReturn(commits);


这样就避免了库测试。

如果您想真正调查库的功能-它发送的请求是什么以及正在发生什么-我建议您遍历库代码,找到实际设置请求的位置,然后以调试模式运行应用程序并在那里添加一个断点。

如果您确实要处理(例如日志,复制...)甚至在生产中对GitHub的实际请求,您都可以使用AspectJ。这将更改字节码,以便您可以包装特定的方法调用。同样,您将不得不在GitHub库中找到执行真正的GitHub(我想是HTTP)调用的位置,并将方面附加到该调用。在一方面,您基本上声明了要拦截的方法以及在调用之前或之后要执行的操作。我不是这方面的专家,所以我无法为您提供更多信息-有一个有关Aspects的教程-https://eclipse.org/aspectj/doc/next/progguide/starting.html

其他选项还可以安装一些网络监视工具,例如wireshark

编辑

避免对GitHub进行昂贵调用的另一种方法是创建一个简单的包装,如下所示:

public class GitHubServiceImpl implements GitHubService {
    private Rpository repo;

    /** other methods **/

    @Override
    public List<RepositoryCommit> getAllCommits(String path) {
        return commitService.getCommits(repo, null, path));
    }
}


这是您要测试的实际服务。

public class CommitService {

    private GitHubService gitHubService;

    private String path;

    public List<RepositoryCommit> getAll() {
        return gitHubService.getAllCommits(path);
    }

}


对于集成测试,请创建GitHubService接口的特殊实现:

public class TestGitHubService implements GitHubService {

    private Map<String, RepositoryCommit> preArrangedCommits;
    /** other methods **/

    public void setPreArrangedCommits(Map<String, RepositoryCommit> preArrangedCommits) {
        this.preArrangedCommits = preArrangedCommits;
    }

    @Override
    public List<RepositoryCommit> getAllCommits(String path) {
        return preArrangedCommits;
    }
}


并在测试调用中:

public class CommitServiceIntegrationTest {
    // @Autowired, @Bean maybe?
    private CommitService commitService;

    // @Autowired, @Bean maybe?
    private GitHubService gitHubService;

    public void testGetAll() {
        Map<String, RepositoryCommit> preArrangedCommits = new HashMap<>();
        preArrangedCommits.put("path/", new RepositoryCommit(...));
        gitHubService.setPreArrangedCommits(preArrangedCommits);
        List<RepositoryCommit> commits = commitService.getAll();
        assertEquals(commits, preArrangedCommits);
    }
}


我会这样走。

坦率

09-10 03:10
查看更多