test:
Add unit tests for primaryskillcontrolle endpoints with mock validationpull/1/head
parent
f7cbdd5f99
commit
c97b33fdc3
|
@ -0,0 +1,140 @@
|
|||
package com.maradona.backend.controllers.api;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.maradona.backend.entities.PrimarySkill;
|
||||
import com.maradona.backend.services.PrimarySkillService;
|
||||
import com.maradona.backend.services.SecondarySkillService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.*;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
|
||||
public class PrimarySkillControllerTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Mock
|
||||
private PrimarySkillService primarySkillService;
|
||||
|
||||
@Mock
|
||||
private SecondarySkillService secondarySkillService;
|
||||
|
||||
@InjectMocks
|
||||
private PrimarySkillController primarySkillController;
|
||||
|
||||
private PrimarySkill primarySkill;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(primarySkillController).build();
|
||||
|
||||
// Initialisieren eines Beispiels für PrimarySkill
|
||||
primarySkill = new PrimarySkill();
|
||||
primarySkill.setPsid(1L);
|
||||
primarySkill.setDescription("Test Primary Skill");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrimarySkillById_Success() throws Exception {
|
||||
// Mock das Service, um ein PrimarySkill zu liefern
|
||||
Mockito.when(primarySkillService.getPrimarySkillById(1L)).thenReturn(Optional.of(primarySkill));
|
||||
|
||||
// Führe die GET-Anfrage aus
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill")
|
||||
.param("id", "1"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
|
||||
|
||||
// Verifiziere, dass der Service aufgerufen wurde
|
||||
Mockito.verify(primarySkillService, Mockito.times(1)).getPrimarySkillById(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllPrimarySkills() throws Exception {
|
||||
// Mock das Service, um eine Liste von PrimarySkills zu liefern
|
||||
Mockito.when(primarySkillService.getAllPrimarySkills()).thenReturn(List.of(primarySkill));
|
||||
|
||||
// Führe die GET-Anfrage aus
|
||||
mockMvc.perform(MockMvcRequestBuilders.get("/api/primary-skill/all"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[0].psid").value(1))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[0].description").value("Test Primary Skill"));
|
||||
|
||||
// Verifiziere, dass der Service aufgerufen wurde
|
||||
Mockito.verify(primarySkillService, Mockito.times(1)).getAllPrimarySkills();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatePrimarySkill() throws Exception {
|
||||
// Mock das Service, um das PrimarySkill zu speichern
|
||||
Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill);
|
||||
|
||||
// Führe die POST-Anfrage aus
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/api/primary-skill")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(new ObjectMapper().writeValueAsString(primarySkill))) // Hier wird das PrimarySkill als JSON übermittelt
|
||||
.andExpect(MockMvcResultMatchers.status().isCreated()) // Erwartet den Statuscode 201 Created
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1)) // Überprüft das psid in der Antwort
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill")); // Überprüft die Beschreibung
|
||||
|
||||
// Verifiziere, dass der Service aufgerufen wurde
|
||||
Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUpdatePrimarySkill() throws Exception {
|
||||
// Mock das Service, um das PrimarySkill zu speichern
|
||||
Mockito.when(primarySkillService.savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class))).thenReturn(primarySkill);
|
||||
|
||||
// Führe die PUT-Anfrage aus
|
||||
mockMvc.perform(MockMvcRequestBuilders.put("/api/primary-skill")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(new ObjectMapper().writeValueAsString(primarySkill)))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.psid").value(1))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.description").value("Test Primary Skill"));
|
||||
|
||||
// Verifiziere, dass der Service aufgerufen wurde
|
||||
Mockito.verify(primarySkillService, Mockito.times(1)).savePrimarySkill(ArgumentMatchers.any(PrimarySkill.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePrimarySkill_Success() throws Exception {
|
||||
// Mock das Service, um das PrimarySkill zu liefern
|
||||
Mockito.when(primarySkillService.getPrimarySkillById(1L)).thenReturn(Optional.of(primarySkill));
|
||||
|
||||
// Führe die DELETE-Anfrage aus
|
||||
mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill")
|
||||
.param("id", "1"))
|
||||
.andExpect(MockMvcResultMatchers.status().isNoContent());
|
||||
|
||||
// Verifiziere, dass der Service aufgerufen wurde
|
||||
Mockito.verify(primarySkillService, Mockito.times(1)).deletePrimarySkill(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePrimarySkill_NotFound() throws Exception {
|
||||
// Mock das Service, um das PrimarySkill nicht zu finden
|
||||
Mockito.when(primarySkillService.getPrimarySkillById(1L)).thenReturn(Optional.empty());
|
||||
|
||||
// Führe die DELETE-Anfrage aus
|
||||
mockMvc.perform(MockMvcRequestBuilders.delete("/api/primary-skill")
|
||||
.param("id", "1"))
|
||||
.andExpect(MockMvcResultMatchers.status().isNotFound());
|
||||
|
||||
// Verifiziere, dass der Service nicht aufgerufen wurde
|
||||
Mockito.verify(primarySkillService, Mockito.never()).deletePrimarySkill(1L);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue