Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
1.5 kB
1
Indexable
Never
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
    }
}
Leave a Comment