java - 如何在 Spring 中调用外部 API 来测试服务

标签 java spring-boot microsoft-graph-api junit5 wiremock

好吧,总的来说,我对测试和 Spring Boot 还很陌生,所以如果我首先在这里做了一些完全错误的事情,请纠正我。

作为一个项目,我和我的团队正在使用 Spring Boot 制作一个 Web 应用程序,我们在一些服务中调用 Microsoft Graph API。请参阅此服务以取消用户日历中的 Activity :

import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.requests.extensions.GraphServiceClient;
import org.springframework.stereotype.Service;


@Service
public class CancelEventService {

    public void cancelEvent(final String token, final String id) {

        IAuthenticationProvider authenticationProvider = iHttpRequest -> iHttpRequest.addHeader("Authorization", "Bearer " + token);
        IGraphServiceClient graphServiceClient = GraphServiceClient.builder().authenticationProvider(authenticationProvider).buildClient();

        graphServiceClient.me().events(id)
                .buildRequest()
                .delete();
    }
}

这工作得很好,但是几天来我一直在努力解决如何为此编写单元测试的问题。在我看来,我想要模拟 GraphServiceClient,或者使用像 WireMock 这样的工具将请求发送到模拟服务器并返回我配置的一些值。

我已经尝试同时执行这两种操作,但我无法模拟 GraphServiceClient,因为它不是我项目中的 Bean,所以我不知道应该如何继续进行实现,我可以 Autowiring 到我的项目中服务。

当谈到 WireMock 时,我什至不确定我是否理解它是否能够完成我想做的事情,如果是的话,我肯定无法正确配置它(请注意,我们正在使用 JUnit 5对于这个项目)。一个简单的示例,您向 Google.com 发出 GET 请求并通过 WireMock 返回“Hello”就足以让我转到这里。

任何关于我应该做什么的具体例子,甚至只是在正确的方向上点头,我们都将不胜感激。

最佳答案

嗯,我不能向您保证它会起作用,但它会让您更好地了解情况:

1) 首先,我们需要对您的服务进行一些细微的改变。 需要从方法中提取 IGraphServiceClient 以便我们稍后可以模拟它,请参阅:

@Service
public class CancelEventService {

   private IGraphServiceClient graphServiceClient;

   @Autowired
   public CancelEventService(IGraphServiceClient graphServiceClient){
       this.graphServiceClient = graphServiceClient;
   }

    public void cancelEvent(final String token, final String id) {

        IAuthenticationProvider authenticationProvider = iHttpRequest -> iHttpRequest.addHeader("Authorization", "Bearer " + token);
        graphServiceClient = GraphServiceClient.builder().authenticationProvider(authenticationProvider).buildClient();

        graphServiceClient.me().events(id)
                .buildRequest()
                .delete();
    }
}

2) 测试如下所示: (请注意,我们在这里使用的所有内容都包含在 Spring Boot 测试模块中,因此您不需要向项目依赖项添加任何内容)

@RunWith(SpringRunner.class)
public class CancelEventServiceTest {

  private IGraphServiceClient graphServiceClientMock;

  private CancelEventService serviceToBeTested;

  @Before
  public void setUp(){

    graphServiceClientMock = Mockito.mock(IGraphServiceClient.class, RETURNS_DEEP_STUBS);
    serviceToBeTested = new CancelEventService(graphServiceClientMock);
  }

  @Test
  public void test_1() {


    serviceToBeTested.cancelEvent("token", "id");

    verify(graphServiceClientMock, times(1)).me().events("id").buildRequest()
        .delete();
  }
}

希望对你有帮助!

关于java - 如何在 Spring 中调用外部 API 来测试服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60835083/

相关文章:

c# - Microsoft.Graph api 设置用户扩展属性

java - 从 Java 计算的 HMAC 值与 Ruby 代码不匹配

java - 如何在Spring Boot aop @Around函数中创建事务?

office365 - Office 365 API Ruby 示例应用程序已损坏

java - Spring Boot 和 ActiveMQ : Ignores broker-url and uses default localhost:61616

java - 在 Spring Boot 中通过 @Value 检索 application.properties 值

c# - MS Graph API C# 将用户添加到组

java - 在 Internet 上搜索可用的 Java 类?

java - CheckingButton 后台资源 Id android

java - Mule TransformerException : An invalid return type "class [B" was specified for transformer "JAXBMarshallerTransformer"