Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
223: Contract Testing Between Scala Microservices
Why can't I just rely on integration tests for this?
I get this question all the time. In a perfect world, spinning up every microservice in a staging environment and running a suite of integration tests would be enough. But as your system grows, that approach becomes a nightmare. You end up with a "distributed monolith" where you can't deploy the PaymentService because the OrderService is currently broken in staging, which blocks your tests.
Integration tests are slow and flaky. Contract testing flips the script. Instead of testing the implementation of two services together, we test the agreement between them. We verify that the Consumer (the one calling the API) and the Provider (the one serving the API) still speak the same language. If the PaymentService changes a field from transaction_id to tx_id, a contract test will catch that in the Provider's CI pipeline long before it ever reaches a shared environment.
How do I actually implement a "Consumer-Driven" contract in Scala?
The "Consumer-Driven" part means the service that needs the data defines the requirements. In the Scala ecosystem, we typically use Pact. The consumer writes a test that defines the expected response and then generates a "pact file" (a JSON contract).
Imagine our OrderService needs to check a payment status. Instead of mocking the PaymentService with a hardcoded string that might be outdated, we define the expectation. Here is a simplified look at how you'd structure that expectation using a Pact-like DSL in Scala:
import au.com.saxon.pact.consumer.PactDsl
import au.com.saxon.pact.consumer.Pact
// Define the contract for the Payment Status endpoint
val paymentContract = Pact.create("OrderService", "PaymentService")
.uponReceiving("A request for payment status")
.path("/payments/status/123")
.method("GET")
.willRespondWith()
.status(200)
.body(PactDsl.newJsonBody()
.stringType("paymentId", "PAY-123")
.stringType("status", "COMPLETED")
.integerType("amountCents", 5000))
.toPact()
The key here is stringType. We aren't testing for the exact value "PAY-123", but rather that the field paymentId exists and is a string. This keeps the contract flexible but firm on the data types.
What happens on the Provider side to make this useful?
Generating the JSON file is only half the battle. The real magic happens when the PaymentService (the Provider) pulls that pact file during its own build process. The Provider doesn't need the Consumer to be running; it just needs the contract.
The Provider's test runner reads the pact file, plays the requested call (GET /payments/status/123) against the local running instance of the service, and compares the actual response to the expected one. If the Provider's code returns an integer for paymentId instead of a string, the test fails.
I've seen teams try to do this manually with shared JSON schemas, but that usually fails because schemas are often too broad. Consumer-driven contracts are better because they only test the fields the consumer actually uses. If the Provider adds ten new fields to the response, the contract test still passes because the Consumer doesn't care about those extra fields. It only cares that the ones it depends on are still there.
How do I handle versioning when the contract has to change?
This is where things get spicy. You can't just change the contract and break the Provider's build; you'll stop everyone from deploying. The workflow should be:
- The Consumer updates the contract to include a new field or change a type.
- The Consumer publishes the new version of the pact (usually to a "Pact Broker").
- The Provider sees the new contract failing in their CI.
- The Provider implements the change to satisfy the new contract while maintaining backward compatibility for older versions of the consumer.
- Once the Provider is updated, the Consumer deploys the version of the code that relies on the new contract.
It sounds like a lot of overhead, but it's a thousand times better than the "deploy and pray" method where you find out about a breaking change via a 500-error spike in your production logs.
📋 Practical Task
Exercise: Breaking and Fixing the Payment-Order Contract
You have been provided with a Scala project containing two modules: order-service (Consumer) and payment-service (Provider). Currently, they are in sync, and the contract tests pass.
Your goal:
- Navigate to the
payment-serviceand rename the JSON fieldamountCentstototalAmountCentsin the response case class. - Run the Provider verification tests and observe the contract failure. Analyze the error message to identify exactly which field the
order-serviceis missing. - Instead of just renaming it back, implement a "grace period" strategy: modify the
payment-serviceto return both the oldamountCentsand the newtotalAmountCentsfields in the JSON response. - Run the verification tests again to ensure the contract is satisfied while the Provider evolves its API.
There are no comments for now.