This test sends a GET request to /api/hello and checks if the response is 200 OK with body "Hello World".
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.ResponseEntity;
import org.springframework.http.HttpStatus;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ApiTest {
private final TestRestTemplate restTemplate = new TestRestTemplate();
@Test
public void testGetHello() {
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/api/hello", String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals("Hello World", response.getBody());
}
}