-
Notifications
You must be signed in to change notification settings - Fork 0
Testing Instructions
Here's a comprehensive test plan using curl commands to verify that your application is functioning correctly. You can run these commands from your terminal to test each endpoint.
curl -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d '{"name":"MongoDB Book","description":"A comprehensive guide to MongoDB","price":29.99}'
Expected response: A JSON object containing the created product with an auto-generated ID:
{"id":"64f1a2b3c4d5e6f7a8b9c0d1","name":"MongoDB Book","description":"A comprehensive guide to MongoDB","price":29.99}
curl -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d '{"name":"Spring Boot Course","description":"Learn Spring Boot","price":49.99}'
curl http://localhost:8080/api/products
Expected response: A JSON array containing all products you've created.
Replace YOUR_PRODUCT_ID
with an actual ID from the previous responses:
curl http://localhost:8080/api/products/YOUR_PRODUCT_ID
Expected response: The specific product matching that ID.
Replace YOUR_PRODUCT_ID
with an actual ID:
curl -X PUT http://localhost:8080/api/products/YOUR_PRODUCT_ID \
-H "Content-Type: application/json" \
-d '{"name":"Updated MongoDB Book","description":"A revised guide to MongoDB","price":34.99}'
Expected response: The updated product details.
Replace YOUR_PRODUCT_ID
with an actual ID:
curl -X DELETE http://localhost:8080/api/products/YOUR_PRODUCT_ID
Expected response: HTTP 204 No Content (empty response with status code 204).
curl "http://localhost:8080/api/products/search?name=Spring"
Expected response: Products containing "Spring" in their name.
curl "http://localhost:8080/api/products/search?maxPrice=40"
Expected response: Products with price less than or equal to 40.
If you want to verify the data directly in MongoDB, you can use the MongoDB shell:
docker exec -it mongodb mongosh
Then run these commands in the MongoDB shell:
use mydatabase
db.products.find() // Shows all products
db.products.count() // Shows count of products
curl -v http://localhost:8080/api/products/nonexistentid
Expected response: HTTP 404 Not Found.
curl -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d '{"name":"Incomplete Product"}'
This should still work since MongoDB is schema-less, but the price will be null.
For a simple load test, you can create a bash script to send multiple requests:
#!/bin/bash
for i in {1..10}; do
curl -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d "{\"name\":\"Product $i\",\"description\":\"Test product $i\",\"price\":$i.99}"
echo ""
done
Save this as load-test.sh
, make it executable with chmod +x load-test.sh
, and run it with ./load-test.sh
.
If you encounter issues:
-
Connection refused: Ensure MongoDB is running with
docker ps
- No data returned: Check your application logs for errors
- 404 Not Found on all endpoints: Verify your application is running and the controller is properly configured
- Missing data in responses: Check field names match between your model and JSON payload
To verify indexes are working as expected, you can check the execution plan of a query in MongoDB:
use mydatabase
db.products.find({name: "MongoDB Book"}).explain("executionStats")
If your application has defined indexes on the name
field, you should see it being used in the execution plan.
This script performs a complete test of all CRUD operations in sequence:
#!/bin/bash
# Create a product
echo "Creating product..."
RESPONSE=$(curl -s -X POST http://localhost:8080/api/products \
-H "Content-Type: application/json" \
-d '{"name":"Test Suite Product","description":"Testing full cycle","price":39.99}')
echo $RESPONSE
# Extract ID from response
ID=$(echo $RESPONSE | grep -o '"id":"[^"]*' | sed 's/"id":"//')
echo "Created product with ID: $ID"
# Get all products
echo -e "\nGetting all products..."
curl -s http://localhost:8080/api/products
# Get the specific product
echo -e "\nGetting product by ID: $ID"
curl -s http://localhost:8080/api/products/$ID
# Update the product
echo -e "\nUpdating product..."
curl -s -X PUT http://localhost:8080/api/products/$ID \
-H "Content-Type: application/json" \
-d '{"name":"Updated Test Product","description":"Updated description","price":49.99}'
# Search for products
echo -e "\nSearching for products with name containing 'Test'..."
curl -s "http://localhost:8080/api/products/search?name=Test"
# Delete the product
echo -e "\nDeleting product..."
curl -s -X DELETE -v http://localhost:8080/api/products/$ID
# Verify it's gone
echo -e "\nVerifying product is deleted..."
curl -s http://localhost:8080/api/products/$ID
Save as test-full-cycle.sh
, make executable with chmod +x test-full-cycle.sh
, and run with ./test-full-cycle.sh
.
This script should give you a comprehensive view of whether your application is working correctly with MongoDB.