Service integration tests
Quick answer
An integration test checks that components work together through their public contracts. In a backend, that can mean routing, request decoding, handler behavior, database effects and response encoding.
Exercise observable behavior
Use controlled request fixtures and an isolated store for each test. A router test using Tower ServiceExt exercises the HTTP service without a listening socket but does not validate TCP, TLS or reverse-proxy behavior. Test happy paths and invalid requests, and verify response body and persisted state where applicable. These manual tests use the prior Axum, Tokio and Tower candidates; put each in a separate integration test file and run cargo test.
use axum::{Router, routing::get, body::{Body, to_bytes}, http::Request};use tower::ServiceExt;#[tokio::test(flavor = "current_thread")]async fn health() { let app = Router::new().route("/health", get(|| async { "ok" })); let r = app.oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()).await.unwrap(); assert_eq!(r.status().as_u16(), 200); assert_eq!(&to_bytes(r.into_body(), 32).await.unwrap()[..], b"ok");}Manual local Rust/Cargo candidate; not executed. Expected behavior:
One passing test if the dependencies resolve.The body read has an explicit upper limit.
use axum::{Router, routing::get, body::Body, http::Request};use tower::ServiceExt;#[tokio::test(flavor = "current_thread")]async fn wrong_method() { let app = Router::new().route("/health", get(|| async { "ok" })); let req = Request::builder().method("POST").uri("/health").body(Body::empty()).unwrap(); assert_eq!(app.oneshot(req).await.unwrap().status().as_u16(), 405);}Manual local Rust/Cargo candidate; not executed. Expected behavior:
One passing test if the dependencies resolve.The route exists but does not accept POST.
Common mistake
Do not rely only on successful status codes; test contract failures and state changes too.
Try it yourself
Add an invalid JSON request test and assert that the store remains unchanged.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service integration tests — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Router::oneshot tests pass. Which layer is still not exercised by those tests?
Service integration tests — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use axum::{Router, routing::get, body::{Body, to_bytes}, http::Request}; use tower::ServiceExt; #[tokio::test(flavor = "current_thread")] async fn health() { let app = Router::new().route("/health", get(|| async { "ok" })); let r = app.oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap()).await.unwrap(); assert_eq!(r.status().as_u16(), 200); assert_eq!(&to_bytes(r.into_body(), 32).await.unwrap()[..], b"ok"); } ``` Which outcome or interpretation best matches this example?
Service integration tests — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use axum::{Router, routing::get, body::Body, http::Request}; use tower::ServiceExt; #[tokio::test(flavor = "current_thread")] async fn wrong_method() { let app = Router::new().route("/health", get(|| async { "ok" })); let req = Request::builder().method("POST").uri("/health").body(Body::empty()).unwrap(); assert_eq!(app.oneshot(req).await.unwrap().status().as_u16(), 405); } ``` Which outcome or interpretation best matches this example?