This guide describes how to test the Coffee Order System.
The Coffee Order System uses the following testing approaches:
- Unit Testing: Testing individual components in isolation.
- Integration Testing: Testing the interaction between components.
- End-to-End Testing: Testing the entire system.
Unit tests are written using Go's built-in testing package. They test individual components in isolation, using mocks for dependencies.
To run all unit tests:
go test ./...To run tests for a specific package:
go test ./handlerTo run a specific test:
go test -run TestPlaceOrder ./handlerUnit tests are located in files with the _test.go suffix. Here's an example of a unit test for the PlaceOrder handler:
func TestPlaceOrder(t *testing.T) {
// Create a mock producer
mockProducer := &MockProducer{
PushToQueueFunc: func(topic string, message []byte) error {
return nil
},
}
// Create a test configuration
cfg := &config.Config{
Kafka: config.KafkaConfig{
Topic: "test_topic",
},
}
// Create a handler with the mock producer
h := NewHandler(mockProducer, cfg)
// Create a test order
order := Order{
CustomerName: "Test Customer",
CoffeeType: "Test Coffee",
}
// Convert the order to JSON
orderJSON, err := json.Marshal(order)
if err != nil {
t.Fatalf("Failed to marshal order: %v", err)
}
// Create a test request
req, err := http.NewRequest("POST", "/order", bytes.NewBuffer(orderJSON))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
// Create a test response recorder
rr := httptest.NewRecorder()
// Call the handler
h.PlaceOrder(rr, req)
// Check the status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// Check the response body
var response Response
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if !response.Success {
t.Errorf("Handler returned wrong success value: got %v want %v", response.Success, true)
}
expectedMessage := "Order for Test Customer placed successfully!"
if response.Message != expectedMessage {
t.Errorf("Handler returned wrong message: got %v want %v", response.Message, expectedMessage)
}
}Integration tests test the interaction between components. For example, testing the interaction between the Producer service and Kafka.
Integration tests are not currently implemented in the Coffee Order System. However, they could be implemented using Docker Compose to set up a test environment with Kafka.
End-to-end tests test the entire system, from the API to the Consumer service.
End-to-end tests are not currently implemented in the Coffee Order System. However, they could be implemented using tools like Postman or custom scripts.
To check test coverage:
go test -cover ./...To generate a detailed coverage report:
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.outThe Coffee Order System uses interface-based design to make mocking easier. For example, the kafka.Producer interface can be mocked for testing:
// MockProducer is a mock implementation of the kafka.Producer interface
type MockProducer struct {
PushToQueueFunc func(topic string, message []byte) error
CloseFunc func() error
}
func (m *MockProducer) PushToQueue(topic string, message []byte) error {
if m.PushToQueueFunc != nil {
return m.PushToQueueFunc(topic, message)
}
return nil
}
func (m *MockProducer) Close() error {
if m.CloseFunc != nil {
return m.CloseFunc()
}
return nil
}The project does not currently have a CI/CD pipeline set up. However, you can run the following commands locally to ensure your changes meet the project standards:
# Format code
go fmt ./...
# Run tests
go test ./...
# Check for common mistakes
go vet ./...- Development Guide: Learn how to develop the system.
- Troubleshooting: Troubleshoot testing issues.
- API Reference: Explore the API endpoints.