Skip to content

Instantly share code, notes, and snippets.

@up1
Created December 2, 2025 09:15
Show Gist options
  • Select an option

  • Save up1/fae35340400b616c6d981c2453a3f045 to your computer and use it in GitHub Desktop.

Select an option

Save up1/fae35340400b616c6d981c2453a3f045 to your computer and use it in GitHub Desktop.
Spring Boot test in Spring boot 4.0.0
package com.example.demotest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureRestTestClient;
import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.web.servlet.client.RestTestClient;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureTestRestTemplate
@AutoConfigureRestTestClient
class HelloControllerTest {
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private RestTestClient restTestClient;
@Test
void sayHiWithTestRestTemplate() {
var response = testRestTemplate.getForObject("/hello", Message.class);
assertEquals("Hello Testing with Spring Boot 4.0.0", response.message());
}
@Test
void sayHiWithRestTestClient() {
restTestClient
.get()
.uri("/hello")
.exchange()
.expectStatus().is2xxSuccessful()
.expectBody(Message.class)
.isEqualTo(new Message("Hello Testing with Spring Boot 4.0.0"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment