Skip to content

Testing Instructions

Maruf Bepary edited this page Mar 12, 2025 · 1 revision

Testing Your Spring Boot MongoDB Application

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.

Basic CRUD Operations Testing

1. Create a Product (POST)

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}

2. Create Another Product

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}'

3. Get All Products (GET)

curl http://localhost:8080/api/products

Expected response: A JSON array containing all products you've created.

4. Get Product by ID (GET)

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.

5. Update a Product (PUT)

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.

6. Delete a Product (DELETE)

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).

Search Functionality Testing

7. Search Products by Name

curl "http://localhost:8080/api/products/search?name=Spring"

Expected response: Products containing "Spring" in their name.

8. Search Products by Max Price

curl "http://localhost:8080/api/products/search?maxPrice=40"

Expected response: Products with price less than or equal to 40.

Verifying in MongoDB

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

Testing Invalid Cases

9. Get Non-existent Product

curl -v http://localhost:8080/api/products/nonexistentid

Expected response: HTTP 404 Not Found.

10. Create Product with Missing Fields

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.

Load Testing (Optional)

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.

Troubleshooting

If you encounter issues:

  1. Connection refused: Ensure MongoDB is running with docker ps
  2. No data returned: Check your application logs for errors
  3. 404 Not Found on all endpoints: Verify your application is running and the controller is properly configured
  4. Missing data in responses: Check field names match between your model and JSON payload

Validating MongoDB Indexes (Optional)

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.

Complete Full-Cycle Test

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.