Untitled
unknown
plain_text
a year ago
1.5 kB
7
Indexable
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyRestControllerIntegrationTest {
private final TestRestTemplate restTemplate = new TestRestTemplate();
@Test
public void testPostRequestWithBody() {
// Define your request body
String requestBody = "{\"key\": \"value\"}";
// Set headers if needed
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
// Create HttpEntity with headers and body
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
// Send POST request and get response
ResponseEntity<String> responseEntity = restTemplate.exchange(
"/api/endpoint", // Replace with your API endpoint
HttpMethod.POST,
requestEntity,
String.class);
// Verify response
assertEquals(200, responseEntity.getStatusCodeValue());
// Add more assertions as needed
}
}
Editor is loading...
Leave a Comment