Skip to content

grace-guides/gs-spring-boot-test

Repository files navigation

Main branch build status Apache 2.0 license Grace on X

Groovy Version Grace Version Spring Boot Version

Use @SpringBootTest with Grace apps

This guide walks you through the process of creating a Grace application and then testing it with JUnit.

Creating a new Grace app

$ sdk install grace 2023.3.0-M1
$ sdk use grace 2023.3.0-M1

$ grace create-app grace.guides.gs-spring-boot-test

Add test dpendency

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")
    }

}

Running test

$ ./gradlew test

Links

About

Use `@SpringBootTest` with Grace apps

Resources

Stars

Watchers

Forks

Releases

No releases published