saveTest() and updateTest() in AdviceController
package greencity.controller;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import greencity.dto.advice.AdvicePostDTO;
import greencity.service.AdviceService;
-import greencity.service.LanguageService;
-import greencity.validator.LanguageTranslationValidator;
+import greencity.service.AdviceTranslationService;
import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.ArgumentMatchers.eq;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.modelmapper.ModelMapper;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
-import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.validation.Validator;
-@ExtendWith(SpringExtension.class)
+@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class AdviceControllerTest {
private static final String adviceLink = "/advices";
@@ -38,10 +38,10 @@ class AdviceControllerTest {
ModelMapper modelMapper;
@Mock
- LanguageService languageService;
+ AdviceTranslationService adviceTranslationService;
@Mock
- LanguageTranslationValidator languageTranslationValidator;
+ Validator mockValidator;
@InjectMocks
AdviceController adviceController;
@@ -79,6 +79,7 @@ class AdviceControllerTest {
void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(adviceController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
+ .setValidator(mockValidator)
.build();
}
@@ -105,25 +106,29 @@ class AdviceControllerTest {
}
@Test
- @Disabled
public void saveTest() throws Exception {
- //AdvicePostDTO advicePostDTO = ModelUtils.getAdvicePostDTO();
-
- //when(languageService.getAllLanguages()).thenReturn(Collections.singletonList(ModelUtils.getLanguageDTO()));
-
mockMvc.perform(post(adviceLink)
.contentType(MediaType.APPLICATION_JSON)
.content(content))
.andExpect(status().isCreated());
+
+ ObjectMapper mapper = new ObjectMapper();
+ AdvicePostDTO advicePostDTO = mapper.readValue(content, AdvicePostDTO.class);
+
+ verify(adviceTranslationService, times(1)).saveAdviceAndAdviceTranslation(advicePostDTO);
}
@Test
- @Disabled
public void updateTest() throws Exception {
mockMvc.perform(put(adviceLink + "/{adviceId}", 1)
.contentType(MediaType.APPLICATION_JSON)
.content(content))
.andExpect(status().isOk());
+
+ ObjectMapper mapper = new ObjectMapper();
+ AdvicePostDTO advicePostDTO = mapper.readValue(content, AdvicePostDTO.class);
+
+ verify(adviceService, times(1)).update(advicePostDTO, 1L);
}
@Test
@@ -143,7 +148,4 @@ class AdviceControllerTest {
verify(adviceService, times(0))
.delete(anyLong());
}
-
-
-
}
\ No newline at end of file
