没有说明 直接上代码 主要的注解

    • @Transactional @Rollback
    1. package com.detabes.appointment.service.impl;
    2. import cn.hutool.core.util.IdUtil;
    3. import com.detabes.appointment.bean.StructureBean;
    4. import com.detabes.appointment.service.StructureService;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.junit.Assert;
    7. import org.junit.jupiter.api.BeforeEach;
    8. import org.junit.jupiter.api.Test;
    9. import org.springframework.beans.factory.annotation.Autowired;
    10. import org.springframework.boot.test.context.SpringBootTest;
    11. import org.springframework.transaction.annotation.Transactional;
    12. import java.util.Arrays;
    13. import java.util.List;
    14. import static org.junit.Assert.assertEquals;
    15. /**
    16. * @link https://blog.csdn.net/xiaolyuh123/article/details/73275283
    17. * <pre>@Rollback(true)// 事务自动回滚,默认是true。可以不写</pre>
    18. * @author tn
    19. */
    20. @SpringBootTest
    21. @Transactional
    22. @Slf4j
    23. public class StructureServiceImplTest {
    24. @Autowired
    25. private StructureService structureService;
    26. private StructureBean structureBean;
    27. @BeforeEach
    28. public void setUp() {
    29. structureBean = new StructureBean();
    30. structureBean.setUuid(IdUtil.simpleUUID());
    31. structureBean.setName("test");
    32. structureBean.setParentUuid(IdUtil.simpleUUID());
    33. structureBean.setTenementUuid(IdUtil.simpleUUID());
    34. structureBean.setRegionUuid(IdUtil.simpleUUID());
    35. structureBean.setStorey(1);
    36. structureBean.setStructureNumber("12");
    37. }
    38. @Test
    39. // @Rollback(value = false) //单元测试数据不保存到数据库
    40. void saveByBean() {
    41. StructureBean saveByBean = structureService.saveByBean(structureBean);
    42. Assert.assertNotNull(saveByBean);
    43. }
    44. @Test
    45. void saveByBoolean() {
    46. assertEquals(structureService.saveByBoolean(structureBean),true);
    47. }
    48. @Test
    49. void deleteById() {
    50. StructureBean saveByBean = structureService.saveByBean(structureBean);
    51. assertEquals(structureService.deleteById(saveByBean.getId()),true);
    52. }
    53. @Test
    54. void testDeleteById() {
    55. StructureBean saveByBean2 = structureService.saveByBean(structureBean);
    56. StructureBean saveByBean1 = structureService.saveByBean(structureBean);
    57. List<Integer> ids = Arrays.asList(saveByBean1.getId(), saveByBean2.getId());
    58. assertEquals(structureService.deleteById(ids),true);
    59. }
    60. @Test
    61. void deleteByUuid() {
    62. StructureBean saveByBean2 = structureService.saveByBean(structureBean);
    63. assertEquals(structureService.deleteByUuid(saveByBean2.getUuid()),true);
    64. }
    65. @Test
    66. void testDeleteByUuid() {
    67. StructureBean saveByBean2 = structureService.saveByBean(structureBean);
    68. StructureBean saveByBean1 = structureService.saveByBean(structureBean);
    69. List<String> ids = Arrays.asList(saveByBean1.getUuid(), saveByBean2.getUuid());
    70. assertEquals(structureService.deleteByUuid(ids),true);
    71. }
    72. @Test
    73. void updateByBean() {
    74. StructureBean saveByBean = structureService.saveByBean(structureBean);
    75. structureBean.setName("test1");
    76. assertEquals(structureService.updateByBean(structureBean),true);
    77. assertEquals(structureService.findById(saveByBean.getId()).getName(),"test1");
    78. }
    79. @Test
    80. void findById() {
    81. StructureBean saveByBean = structureService.saveByBean(structureBean);
    82. assertEquals(structureService.findById(saveByBean.getId()).getName(),"test");
    83. }
    84. @Test
    85. void findByUuid() {
    86. StructureBean saveByBean = structureService.saveByBean(structureBean);
    87. assertEquals(structureService.findByUuid(saveByBean.getUuid()).getUuid(),structureBean.getUuid());
    88. }
    89. @Test
    90. void findByBean() {
    91. StructureBean saveByBean = structureService.saveByBean(structureBean);
    92. StructureBean select = new StructureBean();
    93. select.setName("test");
    94. select.setRegionUuid(structureBean.getRegionUuid());
    95. List<StructureBean> byBean = structureService.findByBean(select);
    96. Assert.assertNotNull(byBean);
    97. }
    98. }