-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
...m/lcaohoanq/demo/file/FileUploadTest.java → ...oanq/demo/domain/file/FileUploadTest.java
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
22 changes: 22 additions & 0 deletions
22
web_frameworks/SpringBoot/src/test/java/com/lcaohoanq/demo/domain/user/UserControllerIT.java
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 |
---|---|---|
@@ -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("[]"); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...pringBoot/src/test/java/com/lcaohoanq/demo/domain/user/UserControllerRestAssuredTest.java
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...rameworks/SpringBoot/src/test/java/com/lcaohoanq/demo/domain/user/UserControllerTest.java
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 |
---|---|---|
@@ -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("[]"))); | ||
} | ||
|
||
} |