Skip to content

Commit 58719bc

Browse files
authored
integrate Pact tests into pipeline (#123)
1 parent 69220ba commit 58719bc

File tree

4 files changed

+149
-3
lines changed

4 files changed

+149
-3
lines changed

.github/workflows/build.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,39 @@ jobs:
6262
run: |
6363
./mvnw -B spring-boot:run &
6464
ab -p src/test/resources/apachebench/create-customer-request.json -T application/json -c 10 -n 1000 http://localhost:8080/api/customers
65+
66+
pact-contract-tests:
67+
runs-on: ubuntu-24.04
68+
timeout-minutes: 10
69+
name: pact-contract-tests
70+
steps:
71+
- name: Checkout code
72+
uses: actions/checkout@v4
73+
74+
- name: Set up JDK
75+
uses: actions/setup-java@v4
76+
with:
77+
java-version: 21
78+
distribution: 'adopt'
79+
java-package: 'jdk'
80+
cache: 'maven'
81+
82+
- name: Start Pact Broker infrastructure
83+
run: docker compose --file spring-boot-example/docker-pact-broker-compose.yml up -d
84+
85+
- name: Run consumer contract tests
86+
working-directory: spring-boot-example
87+
run: ./mvnw -B test -Dtest=StockApiContractTest
88+
89+
- name: Publish pacts to broker
90+
working-directory: spring-boot-example
91+
run: ./mvnw -B pact:publish
92+
93+
- name: Run provider verification tests
94+
working-directory: spring-boot-example
95+
run: ./mvnw -B test -Dtest=StockApiProviderTest -DdisablePactVerification=false
96+
97+
- name: Clean up Docker containers
98+
if: always()
99+
run: |
100+
docker compose --file spring-boot-example/docker-pact-broker-compose.yml down -v

CLAUDE.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Repository Overview
6+
7+
This is the Java Testing Toolbox - a comprehensive educational repository demonstrating 30 testing tools and libraries for Java developers. The codebase contains practical examples for the book "30 Testing Tools & Libraries Every Java Developer Must Know".
8+
9+
## Architecture
10+
11+
### Module Structure
12+
- **spring-boot-example**: Main module containing most testing examples. Each testing tool has its own package under `src/test/java/de/rieckpil/blog/`
13+
- **jakarta-ee-example**: Demonstrates testing in Jakarta EE context, primarily MicroShed Testing
14+
15+
### Test Organization
16+
- Unit tests: `*Test.java` or `*Spec.groovy`
17+
- Integration tests: `*IT.java`
18+
- Web tests: `*WT.java`
19+
- Performance tests: Located in `gatling` package
20+
- All test examples are self-contained within their respective packages
21+
22+
## Essential Commands
23+
24+
### Spring Boot Example
25+
26+
```bash
27+
# Build and run all tests
28+
./mvnw verify
29+
30+
# Run unit tests only
31+
./mvnw test
32+
33+
# Run integration tests only
34+
./mvnw failsafe:integration-test
35+
36+
# Run specific test class
37+
./mvnw test -Dtest=UserRegistrationServiceTest
38+
39+
# Start the application (requires Docker)
40+
docker-compose up -d
41+
./mvnw spring-boot:run
42+
43+
# Run Gatling performance tests (requires running application)
44+
./mvnw gatling:test
45+
46+
# Run mutation testing
47+
./mvnw pitest:mutationCoverage
48+
49+
# Generate JGiven reports
50+
./mvnw jgiven:report
51+
52+
# Run Pact consumer tests
53+
./mvnw test -Dtest=StockApiContractTest
54+
55+
# Publish pacts to broker (requires running Pact Broker)
56+
docker-compose -f docker-pact-broker-compose.yml up -d
57+
./mvnw pact:publish
58+
59+
# Run Pact provider verification tests (by default skipped with system property)
60+
./mvnw test -Dtest=StockApiProviderTest
61+
```
62+
63+
### Jakarta EE Example
64+
65+
```bash
66+
# Build and run all tests
67+
./mvnw verify
68+
69+
# Run unit tests only
70+
./mvnw test
71+
72+
# Run integration tests
73+
./mvnw failsafe:integration-test
74+
75+
# Start Liberty server
76+
./mvnw liberty:run
77+
```
78+
79+
## Key Testing Patterns
80+
81+
### Test Infrastructure
82+
83+
- **Testcontainers**: Used extensively for integration tests requiring databases or external services
84+
- **Docker Compose**: Provides PostgreSQL and other services for local development
85+
- **MockWebServer/WireMock**: For mocking external HTTP services
86+
- **LocalStack**: For testing AWS services locally
87+
88+
### Testing Frameworks Coverage
89+
90+
The repository demonstrates these testing categories:
91+
92+
1. Test Runners: JUnit 4/5, TestNG, Spock
93+
2. Assertions: AssertJ, Hamcrest, JsonPath, XMLUnit, JSONAssert
94+
3. Mocking: Mockito, WireMock, MockWebServer
95+
4. Integration: Testcontainers, REST Assured, MicroShed Testing
96+
5. Web Testing: Selenium, Selenide
97+
6. Performance: Gatling, JMH, JfrUnit
98+
7. Contract Testing: Pact
99+
8. Architecture Testing: ArchUnit
100+
101+
### Important Conventions
102+
103+
- Java 21 is required for all modules
104+
- Docker must be running for integration tests
105+
- Test output is redirected to files in CI/CD environments
106+
- Each testing tool example is isolated in its own package
107+
- Examples demonstrate real-world usage patterns, not just basic syntax

spring-boot-example/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@
381381
<dependencies>
382382
</dependencies>
383383
<configuration>
384+
<systemPropertyVariables>
385+
<disablePactVerification>true</disablePactVerification>
386+
</systemPropertyVariables>
384387
<includes>
385388
<include>**/*Spec.java</include>
386389
<include>**/*Test.java</include>

spring-boot-example/src/test/java/de/rieckpil/blog/pact/StockApiProviderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package au.com.dius.pactworkshop.provider;
1+
package de.rieckpil.blog.pact;
22

33
import au.com.dius.pact.provider.junit5.HttpTestTarget;
44
import au.com.dius.pact.provider.junit5.PactVerificationContext;
@@ -10,16 +10,16 @@
1010
import de.rieckpil.blog.Application;
1111
import org.apache.hc.core5.http.HttpRequest;
1212
import org.junit.jupiter.api.BeforeEach;
13-
import org.junit.jupiter.api.Disabled;
1413
import org.junit.jupiter.api.TestTemplate;
14+
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
1515
import org.junit.jupiter.api.extension.ExtendWith;
1616
import org.springframework.boot.test.context.SpringBootTest;
1717
import org.springframework.boot.test.web.server.LocalServerPort;
1818

1919
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
2020

21-
@Disabled("Requires running Pact Broker, see Docker Compose")
2221
@Provider("stock-api")
22+
@DisabledIfSystemProperty(named = "disablePactVerification", matches = "true", disabledReason = "Requires running Pact Broker, see Docker Compose")
2323
@PactBroker(url = "http://localhost:9292/", authentication = @PactBrokerAuth(username = "pact-sample", password = "pact-sample"))
2424
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = Application.class)
2525
public class StockApiProviderTest {

0 commit comments

Comments
 (0)