Commit d97ecfd3 authored by Angelo Veltens's avatar Angelo Veltens

sample test for greeting controller

parent 4fc2dd2c
......@@ -20,6 +20,7 @@ dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testCompile("org.springframework.boot:spring-boot-starter-test")
}
test {
......
package de.codecentric.ticketscanner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingsController {
@GetMapping
Hello hello() {
return new Hello("Hello World");
}
}
package de.codecentric.ticketscanner;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
class Hello {
private final String greeting;
}
package de.codecentric.ticketscanner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingsHttpIntegrationTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void greetingShouldReturnDefaultMessage() throws Exception {
ResponseEntity response = this.restTemplate.getForEntity("http://localhost:" + port + "/", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo("{\"greeting\":\"Hello World\"}");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment