Skip to content

Commit

Permalink
Merge pull request #1210 from filipamb/spring-async-mvc-fix
Browse files Browse the repository at this point in the history
Fixed performing async requests for MockMvcTestTarget
  • Loading branch information
uglyog authored Sep 21, 2020
2 parents 4259e56 + 49cae23 commit 4250577
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 12 deletions.
1 change: 1 addition & 0 deletions provider/junit5spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation 'org.springframework:spring-test:5.2.3.RELEASE'
implementation 'org.springframework:spring-web:5.2.3.RELEASE'
implementation 'javax.servlet:javax.servlet-api:3.1.0'
implementation 'org.hamcrest:hamcrest:2.1'

testImplementation 'org.springframework.boot:spring-boot-starter-test:2.2.5.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import au.com.dius.pact.provider.ProviderResponse
import au.com.dius.pact.provider.junit5.TestTarget
import mu.KLogging
import org.apache.commons.lang3.StringUtils
import org.hamcrest.core.IsAnything
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.mock.web.MockMultipartFile
import org.springframework.test.web.client.match.MockRestRequestMatchers.anything
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.RequestBuilder
import org.springframework.test.web.servlet.ResultActions
Expand Down Expand Up @@ -153,7 +153,7 @@ class MockMvcTestTarget @JvmOverloads constructor(
val resultActions = mockMvc.perform(requestBuilder)
return if (resultActions.andReturn().request.isAsyncStarted) {
mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(resultActions
.andExpect(MockMvcResultMatchers.request().asyncResult(anything()))
.andExpect(MockMvcResultMatchers.request().asyncResult<Any>(IsAnything()))
.andReturn()))
} else {
resultActions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;

@Provider("myAwesomeService")
@PactFolder("pacts")
Expand All @@ -36,5 +40,14 @@ static class DataResource {
@ResponseStatus(HttpStatus.NO_CONTENT)
void getData(@RequestParam("ticketId") String ticketId) {
}

@GetMapping("/async-data")
DeferredResult<ResponseEntity<Void>> getAsyncData(@RequestParam("ticketId") String ticketId) {
DeferredResult<ResponseEntity<Void>> result = new DeferredResult<>();
CompletableFuture.runAsync(() -> result.setResult(ResponseEntity
.noContent()
.build()));
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package au.com.dius.pact.provider.spring.junit5;

import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.loader.PactFolder;
import au.com.dius.pact.provider.junit5.PactVerificationContext;
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider;
import au.com.dius.pact.provider.junitsupport.Provider;
import au.com.dius.pact.provider.junitsupport.loader.PactFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.CompletableFuture;

@WebMvcTest
@Provider("myAwesomeService")
Expand All @@ -41,5 +45,14 @@ static class DataResource {
@ResponseStatus(HttpStatus.NO_CONTENT)
void getData(@RequestParam("ticketId") String ticketId) {
}

@GetMapping("/async-data")
DeferredResult<ResponseEntity<Void>> getAsyncData(@RequestParam("ticketId") String ticketId) {
DeferredResult<ResponseEntity<Void>> result = new DeferredResult<>();
CompletableFuture.runAsync(() -> result.setResult(ResponseEntity
.noContent()
.build()));
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package au.com.dius.pact.provider.spring.junit5

import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify
import au.com.dius.pact.provider.junitsupport.Provider
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.async.DeferredResult
import java.util.concurrent.CompletableFuture

@Provider("myAwesomeService")
@IgnoreNoPactsToVerify
Expand All @@ -36,5 +39,16 @@ internal class MockMvcTestTargetNoCustomMockMvcTest {
@ResponseStatus(HttpStatus.NO_CONTENT)
fun getData(@RequestParam("ticketId") ticketId: String) {
}

@GetMapping("/async-data")
fun getAsyncData(@RequestParam("ticketId") ticketId: String): DeferredResult<ResponseEntity<Any>> {
val result = DeferredResult<ResponseEntity<Any>>()
CompletableFuture.runAsync {
result.setResult(ResponseEntity
.noContent()
.build())
}
return result
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package au.com.dius.pact.provider.spring.junit5

import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify
import au.com.dius.pact.provider.junitsupport.Provider
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.async.DeferredResult
import java.util.concurrent.CompletableFuture

@Provider("myAwesomeService")
@IgnoreNoPactsToVerify
Expand All @@ -39,5 +42,16 @@ internal class MockMvcTestTargetStandaloneMockMvcTest {
@ResponseStatus(HttpStatus.NO_CONTENT)
fun getData(@RequestParam("ticketId") ticketId: String) {
}

@GetMapping("/async-data")
fun getAsyncData(@RequestParam("ticketId") ticketId: String): DeferredResult<ResponseEntity<Any>> {
val result = DeferredResult<ResponseEntity<Any>>()
CompletableFuture.runAsync {
result.setResult(ResponseEntity
.noContent()
.build())
}
return result
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package au.com.dius.pact.provider.spring.junit5

import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import au.com.dius.pact.provider.junitsupport.IgnoreNoPactsToVerify
import au.com.dius.pact.provider.junitsupport.Provider
import au.com.dius.pact.provider.junitsupport.loader.PactFolder
import au.com.dius.pact.provider.junit5.PactVerificationContext
import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.test.web.servlet.MockMvc
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.async.DeferredResult
import java.util.concurrent.CompletableFuture

@WebMvcTest
@Provider("myAwesomeService")
Expand Down Expand Up @@ -44,4 +47,15 @@ internal class DataResource {
@ResponseStatus(HttpStatus.NO_CONTENT)
fun getData(@RequestParam("ticketId") ticketId: String) {
}

@GetMapping("/async-data")
fun getAsyncData(@RequestParam("ticketId") ticketId: String): DeferredResult<ResponseEntity<Any>> {
val result = DeferredResult<ResponseEntity<Any>>()
CompletableFuture.runAsync {
result.setResult(ResponseEntity
.noContent()
.build())
}
return result
}
}
16 changes: 14 additions & 2 deletions provider/junit5spring/src/test/resources/pacts/contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
"response" : {
"status" : 204
}
} ],
},
{
"description" : "Get async data",
"request" : {
"method" : "GET",
"path" : "/async-data",
"query": "ticketId=0000"
},
"response" : {
"status" : 204
}
}
],
"metadata" : {
"pact-specification" : {
"version" : "2.0.0"
Expand All @@ -24,4 +36,4 @@
"version" : "3.1.1"
}
}
}
}

0 comments on commit 4250577

Please sign in to comment.