Skip to content

Commit

Permalink
throw error if not all message types were tested
Browse files Browse the repository at this point in the history
  • Loading branch information
timvahlbrock committed Jul 14, 2024
1 parent 59c8740 commit 8271134
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import au.com.dius.pact.core.model.V4Interaction
import au.com.dius.pact.core.model.V4Pact
import au.com.dius.pact.core.model.annotations.Pact
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.notNullValue
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.TestTemplate
import org.junit.jupiter.api.extension.ExtendWith
import java.lang.IllegalStateException

@ExtendWith(value = [PactConsumerTestExt::class])
@PactTestFor(providerName = "checkout-service", providerType = ProviderType.ASYNCH, pactVersion = PactSpecVersion.V4)
class TestTemplateTest {
companion object {
private var reservationRan = false
private var cancellationRan = false

private val reservationBody = newJsonBody { o ->
o.stringType("purchaseId", "111")
o.stringType("name", "PURCHASE_STARTED")
Expand Down Expand Up @@ -53,10 +57,25 @@ class TestTemplateTest {
.withContent(cancelationBody)
.toPact()
}

@JvmStatic
@AfterAll
fun makeSureAllRan() {
if(!reservationRan || !cancellationRan) {
throw IllegalStateException("Not all messages were tested.\nReservation: $reservationRan\nCancellation: $cancellationRan")
}
}
}

@TestTemplate
fun testPactForReservationBooking(message: V4Interaction.AsynchronousMessage) {
assertThat(message, `is`(notNullValue()))
if (message.description == "a purchase started message to book a reservation") {
reservationRan = true
} else if(message.description == "a cancellation message to cancel a reservation") {
cancellationRan = true
} else {
throw IllegalArgumentException("Unknown message description")
}
}
}

0 comments on commit 8271134

Please sign in to comment.