[英]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.