Skip to content

Commit

Permalink
test: add test assured
Browse files Browse the repository at this point in the history
  • Loading branch information
lcaohoanq committed Feb 7, 2025
1 parent 00e9305 commit 944c2be
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 17 deletions.
13 changes: 0 additions & 13 deletions web_frameworks/SpringBoot/docker/docker-compose.yml

This file was deleted.

20 changes: 19 additions & 1 deletion web_frameworks/SpringBoot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,25 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ResponseEntity<?> findAll(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size)
{
return ResponseEntity.ok(userRepository.findAll(PageRequest.of(page, size)));
return ResponseEntity.ok(userRepository.findAll(PageRequest.of(page, size)).getContent());
}

@Operation(
Expand Down Expand Up @@ -100,6 +100,6 @@ public ResponseEntity<User> updateUser(
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.delete(id);
return ResponseEntity.ok().build();
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.lcaohoanq.demo.file;
package com.lcaohoanq.demo.domain.file;

import java.io.IOException;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.lcaohoanq.demo.domain.user;

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.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerIT {

@Autowired
private TestRestTemplate template;

@Test
public void getAllUser() throws Exception {
ResponseEntity<String> response = template.getForEntity("/", String.class);
assertThat(response.getBody()).isEqualTo("[]");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.lcaohoanq.demo;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItems;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // Ensures random port
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test") // Ensures the test profile is used
public class UserControllerRestAssuredTest {

@LocalServerPort
private int port;

@BeforeEach
public void setUp() {
RestAssured.port = port;
}

@Test
void getAll() {
given()
.when()
.get("/api/v1/users")
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("", empty()); // Asserting the body is an empty array
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.lcaohoanq.demo.domain.user;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureMockMvc
public class UserControllerTest {

@Autowired
private MockMvc mvc;


@Test
public void getAllUser() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/api/v1/users").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("[]")));
}

}

0 comments on commit 944c2be

Please sign in to comment.