Now that you know how to build microservices, let's see how to do manual testing on your local environment.
The simplest approach to testing NATS-based microservices is to use shell scripts (.sh files) containing NATS CLI commands. These scripts act as a lightweight "Postman collection" that you can run directly from your terminal to test your microservices locally.
Before you can test your microservices, you'll need:
-
NATS CLI installed: Make sure you have the NATS CLI tool installed on your system
# Install NATS CLI (example for macOS) brew install nats-io/nats-tools/nats -
NATS server running: Your local NATS server should be running and accessible
# Check if NATS is running nats server check -
jq installed (optional but recommended): For pretty-printing JSON responses
# Install jq (example for macOS) brew install jq
Create a nats/ directory in your microservice project root to organize your test scripts. Each script should test a specific endpoint or functionality.
Here's the basic structure of a test script:
#!/usr/bin/env bash
# usage: ./nats/health.sh
nats request <subject> <payload> | jqLet's look at a real example from the microservice template:
File: nats/health.sh
#!/usr/bin/env bash
# usage: ./nats/health.sh
nats request health.cloops.microservices.gh.template hello | jqThis script:
- Sends a request to the
health.cloops.microservices.gh.templatesubject - Passes
"hello"as the payload (a simple string) - Pipes the response through
jqfor formatted JSON output
Corresponding Controller:
[NatsConsumer(_subject: "health.cloops.microservices.gh.template")]
public async Task<NatsAck> GetHealth(NatsMsg<string> msg, CancellationToken ct = default)
{
// ... handler logic
return new NatsAck(_isAck: true, _reply: reply);
}For request-reply endpoints, use the nats request command:
#!/usr/bin/env bash
# Test a request-reply endpoint
nats request <subject> <payload> | jqExample with JSON payload:
#!/usr/bin/env bash
# usage: ./nats/get-person.sh
nats request person.get '{"id": "123"}' | jqExample with file payload:
#!/usr/bin/env bash
# usage: ./nats/create-order.sh
nats request order.create "$(cat payloads/create-order.json)" | jqFor publish-subscribe endpoints, use the nats pub command:
#!/usr/bin/env bash
# Test a publish endpoint
nats pub <subject> <payload>Example:
#!/usr/bin/env bash
# usage: ./nats/publish-event.sh
nats pub events.user.created '{"userId": "123", "timestamp": "2024-01-01T00:00:00Z"}'To listen for messages on a subject (useful for debugging):
#!/usr/bin/env bash
# Listen to a subject
nats sub <subject> | jqExample:
#!/usr/bin/env bash
# usage: ./nats/listen-events.sh
nats sub events.user.created | jqKeep all test scripts in a dedicated nats/ directory:
your-microservice/
├── nats/
│ ├── health.sh
│ ├── get-person.sh
│ ├── create-order.sh
│ └── publish-event.sh
└── ...
Ensure your scripts are executable:
chmod +x nats/*.shInclude a usage comment at the top of each script:
#!/usr/bin/env bash
# usage: ./nats/health.sh
# description: Tests the health check endpointAlways pipe responses through jq for readable output:
nats request <subject> <payload> | jqFor complex JSON payloads, consider:
- Using a separate
payloads/directory for JSON files - Using heredoc syntax for multi-line JSON
- Validating JSON before sending
Example with heredoc:
#!/usr/bin/env bash
nats request order.create <<EOF | jq
{
"userId": "123",
"items": [
{"productId": "456", "quantity": 2}
]
}
EOFCreate multiple scripts for different test scenarios:
nats/
├── health.sh # Basic health check
├── get-person-valid.sh # Valid request
├── get-person-invalid.sh # Invalid request (error handling)
└── get-person-missing.sh # Missing data scenario./nats/health.shCreate a simple test runner:
#!/usr/bin/env bash
# usage: ./nats/run-all.sh
for script in nats/*.sh; do
if [ "$(basename $script)" != "run-all.sh" ]; then
echo "Running $(basename $script)..."
$script
echo ""
fi
doneIf you get connection errors, check:
- NATS server is running:
nats server check - Correct NATS server URL (default:
nats://localhost:4222) - Network connectivity
If you get "no responders available":
- Ensure your microservice is running
- Verify the subject name matches exactly
- Check that the consumer is properly registered
If jq fails:
- Validate your JSON payload is correct
- Check for special characters that need escaping
- Try without
jqfirst to see raw output
#!/usr/bin/env bash
# Request with custom timeout (default is 5s)
nats request --timeout 10s <subject> <payload> | jq#!/usr/bin/env bash
# Request with headers
nats request --header "X-Request-ID: test-123" <subject> <payload> | jqFor JetStream subjects, use the nats js commands:
#!/usr/bin/env bash
# Publish to a JetStream subject
nats js pub <subject> <payload>While these scripts are primarily for local manual testing, you can also use them in CI/CD pipelines:
# Example GitHub Actions step
- name: Test microservice endpoints
run: |
chmod +x nats/*.sh
./nats/health.sh
./nats/get-person.sh- Learn about observability to monitor your microservices
- Check out distributed locks for advanced features
- Review additional setup for production configurations