Skip to content
A collection of Pact related libraries
Kotlin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
.mvn/wrapper
pact-provider-junit5
pact-provider
.gitignore
LICENSE
README.md
mvnw
mvnw.cmd
pom.xml

README.md

License Build Status

testIT | Pact Libraries

This repository hosts libraries related to working with Pact (https://docs.pact.io).

Pact Provider JUnit 5

The pact-provider-junit5 module contains a basic provider side test factory implementations for running RequestResponsePact and MessagePact pacts using JUnit 5's @TestFactory mechanism.

Request / Response Pact provider side tests:

@ExtendWith(SpringExtension::class)
@SpringBootTest(classes = [Application::class], webEnvironment = RANDOM_PORT)
class HttpContractTest {

    @MockBean lateinit var dataStore: BookDataStore
    @MockBean lateinit var eventDispatcher: EventDispatcher<BookEvent>

    val pacts = RequestResponsePacts(LocalFiles("src/test/pacts/http"), "library-service")

    @LocalServerPort
    fun init(port: Int) {
        pacts.target.bindPort { port }
    }

    @TestFactory fun `library enrichment contract tests`() =
            PactTestFactory.createTests(pacts, "library-enrichment", this)

    @ProviderState("A book with the ID {bookId} exists")
    fun `book with fixed ID exists`(params: Map<String, String>) {
        val bookId = BookId.from(params["bookId"]!!)
        val bookRecord = BookRecord(bookId, Books.THE_MARTIAN)
        given { dataStore.findById(bookId) }.willReturn { bookRecord }
        given { dataStore.createOrUpdate(any()) }.willAnswer { it.arguments[0] as BookRecord }
    }

}

Message Pact provider side tests:

class MessageContractTest {

    val configuration = MessagingConfiguration()
    val objectMapper = ObjectMapper().apply { findAndRegisterModules() }
    val messageConverter = configuration.messageConverter(objectMapper)

    val pacts = MessagePacts(LocalFiles("src/test/pacts/message"), "library-service")

    @TestFactory fun `library-enrichment consumer contract tests`() =
            PactTestFactory.createTests(pacts, "library-enrichment", this)

    @MessageProducer("'The Martian' was added event")
    fun `verify The Martian was added event`(): ActualMessage {
        val event = BookAdded(
                id = UUID.randomUUID(),
                bookId = BookId.generate(),
                isbn = Books.THE_MARTIAN.isbn,
                timestamp = OffsetDateTime.now()
        )
        val message = messageConverter.toMessage(event, MessageProperties())
        return ActualMessage(message.body)
    }

}

Licensing

TestUtils is licensed under The Apache License, Version 2.0.

Sponsoring

The contents of this repository is mainly developed by NovaTec Consulting GmbH, a German consultancy firm that drives quality in software development projects.

You can’t perform that action at this time.