Testing Patterns
BDD-style testing with Spock Framework for event-driven systems, CQRS services, and Kafka producers.
Spock Framework
Version: Spock 2.4-M7 with Groovy 5.0.0-alpha-11 (Java 25 compatible)
Spock is a testing framework for Java and Groovy applications that combines:
- BDD-style Given-When-Then syntax
- Built-in mocking with Mock() and Stub()
- Expressive assertions and data-driven tests
- Integration with Spring Boot
Test Structure
Backend Tests (27 tests)
- ProductCommandServiceSpec (7 tests): Product creation, update, deletion, stock management
- ProductQueryServiceSpec (7 tests): Product queries and searches
- OrderCommandServiceSpec (5 tests): Order creation and processing
- OrderQueryServiceSpec (7 tests): Order queries by ID, customer, status
- ApplicationSpec (1 test): Application context test
Frontend Tests (12 tests)
- ProductServiceSpec (6 tests): REST client product operations
- OrderServiceSpec (3 tests): REST client order queries
- PurchaseOrderProducerSpec (2 tests): Kafka message publishing
- ApplicationSpec (1 test): Application context test
Example: ProductCommandServiceSpec
class ProductCommandServiceSpec extends Specification {
ProductRepository productRepository = Mock()
ProductCommandService service = new ProductCommandService(productRepository)
def "should create product with valid data"() {
given: "a valid product DTO"
def productDTO = new ProductDTO(
name: "Laptop",
price: 999.99,
stock: 10
)
when: "creating the product"
service.createProduct(productDTO)
then: "repository save is called once"
1 * productRepository.save(_) >> { Product product ->
assert product.name == "Laptop"
assert product.price == 999.99
product
}
}
}
Example: PurchaseOrderProducerSpec
class PurchaseOrderProducerSpec extends Specification {
KafkaTemplate kafkaTemplate = Mock()
PurchaseOrderProducer producer = new PurchaseOrderProducer(kafkaTemplate)
def "should send order to correct Kafka topic"() {
given: "a purchase order"
def order = new PurchaseOrderDTO(
orderId: "ORDER-123",
total: 100.0
)
when: "sending the order"
producer.sendOrder(order)
then: "kafka template sends to purchase-orders topic"
1 * kafkaTemplate.send("purchase-orders", "ORDER-123", order)
}
}
Running Tests
# Run all tests
mvn test
# Run backend tests only
cd spring-boot-kafka-backend && mvn test
# Run frontend tests only
cd spring-boot-kafka-frontend && mvn test
# Run specific test class
mvn test -Dtest=ProductCommandServiceSpec
Java 25 Compatibility
The project uses Groovy 5.0.0-alpha-11, which supports Java 25 bytecode (major version 69). Earlier versions of Groovy do not support Java 25.
Maven Configuration:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>4.0.1</version>
<configuration>
<skipBytecodeCheck>true</skipBytecodeCheck>
</configuration>
</plugin>
Key Takeaways
- Spock provides expressive BDD-style tests
- Mock repositories and external dependencies
- Test CQRS services independently
- Verify Kafka message publishing with mocks