PersonTextControllerSaveOrUpdateTest.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) Andrew Grant of Carrick Software 29/03/2025, 13:55. All rights reserved.
  3. *
  4. */
  5. package scot.carricksoftware.grants.controllers.text.persondocument;
  6. import org.junit.jupiter.api.BeforeEach;
  7. import org.junit.jupiter.api.Test;
  8. import org.junit.jupiter.api.extension.ExtendWith;
  9. import org.mockito.Mock;
  10. import org.mockito.junit.jupiter.MockitoExtension;
  11. import org.springframework.ui.Model;
  12. import org.springframework.validation.BindingResult;
  13. import scot.carricksoftware.grants.commands.text.PersonTextCommand;
  14. import scot.carricksoftware.grants.commands.text.PersonTextCommandImpl;
  15. import scot.carricksoftware.grants.controllers.text.persontext.PersonTextFormControllerImpl;
  16. import scot.carricksoftware.grants.converters.text.persontext.PersonTextCommandConverterImpl;
  17. import scot.carricksoftware.grants.converters.text.persontext.PersonTextConverterImpl;
  18. import scot.carricksoftware.grants.services.people.PersonService;
  19. import scot.carricksoftware.grants.services.text.persontext.PersonTextService;
  20. import scot.carricksoftware.grants.validators.text.PersonTextCommandValidator;
  21. import static org.junit.jupiter.api.Assertions.assertEquals;
  22. import static org.mockito.ArgumentMatchers.any;
  23. import static org.mockito.Mockito.when;
  24. @ExtendWith(MockitoExtension.class)
  25. public class PersonTextControllerSaveOrUpdateTest {
  26. @SuppressWarnings("unused")
  27. private PersonTextFormControllerImpl personTextController;
  28. @Mock
  29. private PersonTextService personTextServiceMock;
  30. @Mock
  31. private PersonTextCommandConverterImpl personTextCommandConverterMock;
  32. @Mock
  33. private PersonTextConverterImpl personTextConverterMock;
  34. @Mock
  35. private Model modelMock;
  36. @Mock
  37. private BindingResult bindingResultMock;
  38. @Mock
  39. private PersonTextCommandValidator personTextCommandValidatorMock;
  40. @Mock
  41. private PersonService personServiceMock;
  42. private PersonTextCommand personTextCommand;
  43. @BeforeEach
  44. public void setUp() {
  45. personTextController = new PersonTextFormControllerImpl(personTextServiceMock,
  46. personTextCommandConverterMock,
  47. personTextConverterMock,
  48. personTextCommandValidatorMock,
  49. personServiceMock);
  50. personTextCommand = new PersonTextCommandImpl();
  51. }
  52. @Test
  53. public void saveOrUpdateNoErrorsTest() {
  54. Long id = 4L;
  55. personTextCommand.setId(id);
  56. when(personTextServiceMock.savePersonTextCommand(any(PersonTextCommand.class))).thenReturn(personTextCommand);
  57. assertEquals("redirect:/personText/4/show", personTextController.saveOrUpdate(personTextCommand, bindingResultMock, modelMock));
  58. }
  59. @Test
  60. public void saveOrUpdateErrorsTest() {
  61. Long id = 4L;
  62. personTextCommand.setId(id);
  63. when(bindingResultMock.hasErrors()).thenReturn(true);
  64. assertEquals("text/personText/form", personTextController.saveOrUpdate(personTextCommand, bindingResultMock, modelMock));
  65. }
  66. }