InfoAddressBookServiceTest.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.ruoyi.app.flashdelivery.service;
  2. import com.ruoyi.app.flashdelivery.dto.InfoAddressRequest;
  3. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  4. import com.ruoyi.system.domain.InfoAddress;
  5. import com.ruoyi.system.service.IInfoAddressService;
  6. import org.junit.jupiter.api.Test;
  7. import static org.junit.jupiter.api.Assertions.assertEquals;
  8. import static org.junit.jupiter.api.Assertions.assertThrows;
  9. import static org.junit.jupiter.api.Assertions.assertTrue;
  10. import static org.mockito.ArgumentMatchers.any;
  11. import static org.mockito.Mockito.mock;
  12. import static org.mockito.Mockito.never;
  13. import static org.mockito.Mockito.verify;
  14. import static org.mockito.Mockito.when;
  15. class InfoAddressBookServiceTest {
  16. @Test
  17. void updateCannotOverwriteAnotherUsersAddress() {
  18. IInfoAddressService addresses = mock(IInfoAddressService.class);
  19. InfoAddress other = new InfoAddress();
  20. other.setId(9L);
  21. other.setUserId(2L);
  22. when(addresses.getById(9L)).thenReturn(other);
  23. InfoAddressRequest request = validRequest();
  24. request.setId(9L);
  25. assertThrows(RuntimeException.class, () -> new InfoAddressBookService(addresses).save(1L, request));
  26. verify(addresses, never()).update(any(), any());
  27. verify(addresses, never()).update(any(Wrapper.class));
  28. }
  29. @Test
  30. void createAlwaysUsesAuthenticatedUser() {
  31. IInfoAddressService addresses = mock(IInfoAddressService.class);
  32. InfoAddressRequest request = validRequest();
  33. new InfoAddressBookService(addresses).save(7L, request);
  34. var captor = org.mockito.ArgumentCaptor.forClass(InfoAddress.class);
  35. verify(addresses).save(captor.capture());
  36. assertEquals(7L, captor.getValue().getUserId());
  37. }
  38. @Test
  39. void updateCanExplicitlyClearOptionalAddressFields() {
  40. IInfoAddressService addresses = mock(IInfoAddressService.class);
  41. InfoAddress existing = new InfoAddress();
  42. existing.setId(9L);
  43. existing.setUserId(1L);
  44. existing.setAddressDetail("old detail");
  45. when(addresses.getById(9L)).thenReturn(existing);
  46. when(addresses.update(any(Wrapper.class))).thenReturn(true);
  47. InfoAddressRequest request = validRequest();
  48. request.setId(9L);
  49. request.setAddressDetail(null);
  50. new InfoAddressBookService(addresses).save(1L, request);
  51. var captor = org.mockito.ArgumentCaptor.forClass(Wrapper.class);
  52. verify(addresses).update(captor.capture());
  53. assertTrue(captor.getValue().getSqlSet().contains("address_detail"));
  54. }
  55. private InfoAddressRequest validRequest() {
  56. InfoAddressRequest request = new InfoAddressRequest();
  57. request.setName("A"); request.setPhone("0911111111"); request.setAddress("road");
  58. request.setLongitude(java.math.BigDecimal.valueOf(121.5));
  59. request.setLatitude(java.math.BigDecimal.valueOf(25));
  60. return request;
  61. }
  62. }