This guide walks you through the process of creating a Grace application and then testing it with JUnit.
$ sdk install grace 2023.3.0-M1
$ sdk use grace 2023.3.0-M1
$ grace create-app grace.guides.gs-spring-boot-test
Add dependency spring-boot-starter-test
to build.grade
,
dependencies {
testImplementation "org.springframework.boot:spring-boot-starter-test"
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}
Creating ApplicationTests
in src/test/groovy/grace/guides
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationTests {
@LocalServerPort
private int port
@Autowired
private TestRestTemplate restTemplate
@Test
void homePage() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/",
String.class)).contains("Welcome to Grace")
}
@Test
void helloWorld() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/hello",
String.class)).contains("Hello, World")
}
}
$ ./gradlew test