简体   繁体   English

为服务编写集成测试

[英]Writing an integration test for a service

I have a service class that I would like to perform the integration test (whitebox). 我有一个要执行集成测试(白盒)的服务类。 The method code, 方法代码,

   @Async( ELLA_THREAD_POOL_EXECUTOR_NAME )
    public void invokeEllaAsync( final IrisBo irisBo ) {

        if( isTrafficAllowed( irisBo ) ) {
            callEllaService( irisBo );
        }
    }

    public void callEllaService( final IrisBo irisBo ) {

        HttpHeaders ellaHeaders = createRequestHeaders( irisBo );

        ServiceResponse<EllaResponseDto> response = connector.call( EllaDtoConverter.convertToRequest( irisBo ), ellaHeaders );

        if( !response.isSuccess() ) {
            LOG.error( "ERROR", response, irisBo );
        }
    }

The method that test is provided below, 下面提供了测试方法,

@Test
public void testCallEllaServiceIsSuccessful() throws IOException {

    String emailAgeResponseJson = readFile( ELLA_RESPONSE_FILE );

    wireMockRule.stubFor( post( urlEqualTo( ELLA_ENDPOINT ) ).willReturn( okJson( emailAgeResponseJson ) ) );

    TestRequestInformation testRequestInformation = new TestRequestInformation();
    IrisBo irisBo = EllaTestDataProvider.createValidIrisBoWithoutRequest();


    service.callEllaService( irisBo );


}

I would like to validate the response data, The method invokeEllaAsync returns void . 我想验证响应数据,方法invokeEllaAsync返回void The response is inside the method callEllaService . 响应位于方法callEllaService

How do I validate the response data? 如何验证响应数据?

First of all, a quick googling reveals this question . 首先,快速浏览便发现了这个问题 Since I'm not 100% you're talking about Spring indeed, I don't suggest to consider this as a duplication. 由于我不是100%的人,实际上的确是在谈论Spring,所以我不建议您将其视为重复。

Now I'll provide a couple of additional techniques, assuming we're talking about Spring's Async methods (the integration test is probably run with SpringRunner ) 现在,假设我们正在谈论Spring的Async方法,我将提供一些其他技术(集成测试可能是通过SpringRunner运行的)

To start from the basics, in a nutshell, @Async methods are executed on the different thread pool. 概括地说, @Async方法在不同的线程池上执行。 Technically it's done by generating a runtime proxy. 从技术上讲,这是通过生成运行时代理来完成的。 So there are many techniques (out of my head, since I didn't really use them) that could help: 因此,有很多技术可以帮助您:

Option 1 选项1

Disable an Async Support for tests. 禁用测试的异步支持。 This can be done by creating some kind of condition on your custom configuration class that will enable async support. 这可以通过在自定义配置类上创建某种类型的条件来实现,以实现异步支持。 Something like this: 像这样:

 @Configuration
 @EnableAsync
 @ConditionalOnProperty(name = "async.support.enabled", havingValue = true)
 public class MyAsyncEnablerConfiguration {

 }

For tests make the variable false 对于测试,使变量为false

Option 2 选项2

If its a method on a controller and you use mockMvc testing (again, only my speculation, I don't know where the code really is), you can take advantage of asyncDispatch method that will take care of asynchronous requests. 如果它是控制器上的一种方法,并且您使用了模拟Mvc测试(再次,仅出于我的推测,我不知道代码的实际位置),则可以利用asyncDispatch方法来处理异步请求。

It looks like this: 看起来像这样:

mockMvc.perform(asyncDispatch(mvcResult)) // <-- Note this call
                    .andExpect(status().isOk())
                ...

A full example can be found Here 完整的例子可以在这里找到

Option 3 选项3

Note that the return type of @Async method doesn't have to be void . 请注意, @Async方法的返回类型不必为void It can be Future or even spring's AsyncResult . 可以是Future甚至是spring的AsyncResult So if its a future - you could call future.get() in the code and get the result. 因此,如果它是未来的话-您可以在代码中调用future.get()并获取结果。

To read about the return result types more check This tutorial 要了解有关返回结果类型的更多信息,请参阅本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Olingo / ODATA 4 编写 ExpressionVisitor 的集成测试 - Writing Integration Test of ExpressionVisitor with Olingo / ODATA 4 在Spring中编写方面功能的集成测试 - Writing an integration test for aspect functionality in Spring 编写服务功能单元测试 - Writing unit test for service function 基于SOAP的Web服务的集成测试 - Integration test for SOAP based web service Spring Boot集成测试Watch Service挣扎 - Spring boot integration test Watch Service Struggles 为另一个Web服务编写测试用例 - Writing test cases for another web service Spring Kafka集成测试写入高水印文件时出错 - Spring Kafka integration test Error while writing to highwatermark file 使用Spring Integration编写合同优先的Web服务 - Writing Contract-First Web Service using Spring Integration 集成测试无法连接,手动浏览器测试通过带有SpringBoot / Jsoup集成测试的Java RESTful Web服务成功 - Integration Tests fail to connect, Manual Browser Test succeeds with Java RESTful Web Service with SpringBoot/Jsoup Integration Test 如何为具有很少依赖关系的服务层编写集成测试 - How to write an integration test for Service layer which have few dependencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM