-
Notifications
You must be signed in to change notification settings - Fork 0
Test ContactController necesita revisión #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
A01661651
wants to merge
1
commit into
main
Choose a base branch
from
CCTests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/test/java/com/itesm/panoptimize/ContactControllerTests.java
This file contains hidden or 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,113 @@ | ||
| package com.itesm.panoptimize; | ||
|
|
||
| import com.itesm.panoptimize.dto.contact.ContactDTO; | ||
| import com.itesm.panoptimize.dto.contact.CreateContactDTO; | ||
| import com.itesm.panoptimize.dto.contact.SearchContactsDTO; | ||
| import com.itesm.panoptimize.service.ContactService; | ||
| import com.itesm.panoptimize.service.ContactSearchService; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
| 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.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageImpl; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
|
||
| import java.util.Collections; | ||
|
|
||
| import static org.hamcrest.Matchers.*; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| public class ContactControllerTests { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @MockBean | ||
| private ContactService contactService; | ||
|
|
||
| @MockBean | ||
| private ContactSearchService contactSearchService; | ||
|
|
||
| private ContactDTO contactDTO; | ||
| private CreateContactDTO createContactDTO; | ||
| private SearchContactsDTO searchContactsDTO; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| contactDTO = new ContactDTO("1", "John Doe", "john.doe@example.com", "1234567890"); | ||
| createContactDTO = new CreateContactDTO("John Doe", "john.doe@example.com", "1234567890"); | ||
| searchContactsDTO = new SearchContactsDTO("John Doe"); | ||
|
|
||
| Mockito.when(contactService.getContact("1")).thenReturn(contactDTO); | ||
| Mockito.when(contactService.createContact(createContactDTO)).thenReturn(contactDTO); | ||
| Mockito.when(contactService.getAllContacts(Mockito.any(Pageable.class))).thenReturn(new PageImpl<>(Collections.singletonList(contactDTO))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetContactById() throws Exception { | ||
| mockMvc.perform(get("/contact/1") | ||
| .contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.id").value(contactDTO.getId())) | ||
| .andExpect(jsonPath("$.name").value(contactDTO.getName())) | ||
| .andExpect(jsonPath("$.email").value(contactDTO.getEmail())) | ||
| .andExpect(jsonPath("$.phone").value(contactDTO.getPhone())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateContact() throws Exception { | ||
| mockMvc.perform(post("/contact/") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{\"name\": \"John Doe\", \"email\": \"john.doe@example.com\", \"phone\": \"1234567890\"}")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.id").value(contactDTO.getId())) | ||
| .andExpect(jsonPath("$.name").value(contactDTO.getName())) | ||
| .andExpect(jsonPath("$.email").value(contactDTO.getEmail())) | ||
| .andExpect(jsonPath("$.phone").value(contactDTO.getPhone())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeleteContact() throws Exception { | ||
| mockMvc.perform(MockMvcRequestBuilders.delete("/contact/1") | ||
| .contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isNoContent()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetAllContacts() throws Exception { | ||
| mockMvc.perform(get("/contact/") | ||
| .contentType(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.content", hasSize(1))) | ||
| .andExpect(jsonPath("$.content[0].id").value(contactDTO.getId())) | ||
| .andExpect(jsonPath("$.content[0].name").value(contactDTO.getName())) | ||
| .andExpect(jsonPath("$.content[0].email").value(contactDTO.getEmail())) | ||
| .andExpect(jsonPath("$.content[0].phone").value(contactDTO.getPhone())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSearchContacts() throws Exception { | ||
| mockMvc.perform(post("/contact/search") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content("{\"query\": \"John Doe\"}")) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.contacts", hasSize(1))) | ||
| .andExpect(jsonPath("$.contacts[0].id").value(contactDTO.getId())) | ||
| .andExpect(jsonPath("$.contacts[0].name").value(contactDTO.getName())) | ||
| .andExpect(jsonPath("$.contacts[0].email").value(contactDTO.getEmail())) | ||
| .andExpect(jsonPath("$.contacts[0].phone").value(contactDTO.getPhone())); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Está bien, pero investiga cómo hacer un test Rollback.