forked from AtomicJar/S123
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add RestAssuredTests (build will fail)
- Loading branch information
Showing
3 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,89 @@ | ||
package com.atomicjar.todos; | ||
|
||
import com.atomicjar.todos.entity.Todo; | ||
import com.atomicjar.todos.repository.TodoRepository; | ||
import io.restassured.RestAssured; | ||
import io.restassured.builder.RequestSpecBuilder; | ||
import io.restassured.http.ContentType; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.util.List; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
classes = {ContainersConfig.class}) | ||
class ApplicationTests { | ||
|
||
@LocalServerPort | ||
protected Integer localServerPort; | ||
protected RequestSpecification requestSpecification; | ||
|
||
|
||
@Autowired | ||
private TodoRepository todoRepository; | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
requestSpecification = new RequestSpecBuilder() | ||
.setPort(localServerPort) | ||
.addHeader( | ||
HttpHeaders.CONTENT_TYPE, | ||
MediaType.APPLICATION_JSON_VALUE | ||
) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void contextLoads() { | ||
// Assertions.assertTrue(false); | ||
List<Todo> customers = List.of( | ||
new Todo(null, "Feed all horses", false, 1), | ||
new Todo(null, "Free all horses", false, 2), | ||
new Todo(null, "Feel all horses", false, 3) | ||
); | ||
todoRepository.saveAll(customers); | ||
|
||
given(requestSpecification) | ||
.when() | ||
.get("/todos") | ||
.then() | ||
.statusCode(200) | ||
.body(".", hasSize(4)); | ||
|
||
todoRepository.deleteAll(); | ||
} | ||
|
||
|
||
@Test | ||
void getByIDFindsId() { | ||
Todo feedAllHorses = todoRepository.save(new Todo(null, "Feed all horses", false, 1)); | ||
|
||
given(requestSpecification) | ||
.when() | ||
.get("/todos/" + feedAllHorses.getId()) | ||
.then() | ||
.statusCode(200) | ||
.body(".", hasSize(4)); | ||
|
||
todoRepository.deleteAll(); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters