diff --git a/src/main/java/com/guo/learningprogresstracker/controller/TestController.java b/src/main/java/com/guo/learningprogresstracker/controller/TestController.java deleted file mode 100644 index 9d38a10..0000000 --- a/src/main/java/com/guo/learningprogresstracker/controller/TestController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.guo.learningprogresstracker.controller; - -import com.guo.learningprogresstracker.entity.CommonResult; -import com.guo.learningprogresstracker.entity.TestTableEntity; -import com.guo.learningprogresstracker.exception.AppException; -import com.guo.learningprogresstracker.exception.ErrorParameterException; -import com.guo.learningprogresstracker.exception.NotFindEntitiesException; -import com.guo.learningprogresstracker.exception.OperationFailedException; -import com.guo.learningprogresstracker.service.TestTableService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@Slf4j -public class TestController { - @Autowired - TestTableService testTableService; - - @GetMapping("/test/{id}") - public CommonResult getTestEntityById(@PathVariable("id") String id) throws Exception { - log.info("testGet请求访问"); - if ("1".equals(id)) { - throw new AppException("测试AppException"); - } else if ("2".equals(id)) { - throw new ErrorParameterException("测试ErrorParameterException"); - }else if ("3".equals(id)) { - throw new NotFindEntitiesException("测试NotFindEntitiesException"); - }else if ("4".equals(id)) { - throw new OperationFailedException("测试OperationFailedException"); - } - TestTableEntity testTableEntity = testTableService.getTestEntityById(id); - return CommonResult.success(testTableEntity); - } -} diff --git a/src/main/java/com/guo/learningprogresstracker/entity/TestTableEntity.java b/src/main/java/com/guo/learningprogresstracker/entity/TestTableEntity.java deleted file mode 100644 index 1e6e9b5..0000000 --- a/src/main/java/com/guo/learningprogresstracker/entity/TestTableEntity.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.guo.learningprogresstracker.entity; - -import com.baomidou.mybatisplus.annotation.IdType; -import com.baomidou.mybatisplus.annotation.TableField; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; -import java.io.Serializable; -import lombok.Data; - -/** - * 测试表 - * @author Guo - * @TableName test_table - */ -@TableName(value ="test_table") -@Data -public class TestTableEntity implements Serializable { - /** - * 测试主键 - */ - @TableId(type = IdType.AUTO) - private Integer id; - - /** - * id名字 - */ - private String idName; - - @TableField(exist = false) - private static final long serialVersionUID = 1L; - - @Override - public boolean equals(Object that) { - if (this == that) { - return true; - } - if (that == null) { - return false; - } - if (getClass() != that.getClass()) { - return false; - } - TestTableEntity other = (TestTableEntity) that; - return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) - && (this.getIdName() == null ? other.getIdName() == null : this.getIdName().equals(other.getIdName())); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); - result = prime * result + ((getIdName() == null) ? 0 : getIdName().hashCode()); - return result; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(getClass().getSimpleName()); - sb.append(" ["); - sb.append("Hash = ").append(hashCode()); - sb.append(", id=").append(id); - sb.append(", idName=").append(idName); - sb.append(", serialVersionUID=").append(serialVersionUID); - sb.append("]"); - return sb.toString(); - } -} \ No newline at end of file diff --git a/src/main/java/com/guo/learningprogresstracker/mapper/TestTableMapper.java b/src/main/java/com/guo/learningprogresstracker/mapper/TestTableMapper.java deleted file mode 100644 index 568f709..0000000 --- a/src/main/java/com/guo/learningprogresstracker/mapper/TestTableMapper.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.guo.learningprogresstracker.mapper; - -import com.guo.learningprogresstracker.entity.TestTableEntity; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import org.apache.ibatis.annotations.Mapper; - - -/** -* @author Administrator -* @description 针对表【test_table(测试表)】的数据库操作Mapper -* @createDate 2024-04-28 18:01:21 -* @Entity generator.domain.TestTable -*/ -@Mapper -public interface TestTableMapper extends BaseMapper { - -} - - - - diff --git a/src/main/java/com/guo/learningprogresstracker/service/TestTableService.java b/src/main/java/com/guo/learningprogresstracker/service/TestTableService.java deleted file mode 100644 index 0ed1cfe..0000000 --- a/src/main/java/com/guo/learningprogresstracker/service/TestTableService.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.guo.learningprogresstracker.service; - -import com.guo.learningprogresstracker.entity.TestTableEntity; -import com.baomidou.mybatisplus.extension.service.IService; - -/** -* @author Administrator -* @description 针对表【test_table(测试表)】的数据库操作Service -* @createDate 2024-04-28 18:01:21 -*/ -//@Service -public interface TestTableService extends IService { - - TestTableEntity getTestEntityById(String id); -} diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImpl.java deleted file mode 100644 index 82af22d..0000000 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImpl.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.guo.learningprogresstracker.service.impl; - -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.guo.learningprogresstracker.entity.TestTableEntity; -import com.guo.learningprogresstracker.mapper.TestTableMapper; -import com.guo.learningprogresstracker.service.TestTableService; -import org.springframework.stereotype.Service; - -/** -* @author Administrator -* @description 针对表【test_table(测试表)】的数据库操作Service实现 -* @createDate 2024-04-28 18:01:21 -*/ -@Service -public class TestTableServiceImpl extends ServiceImpl - implements TestTableService { - - @Override - public TestTableEntity getTestEntityById(String id) { - return this.getById(id); - } -} - - - - diff --git a/src/main/resources/mapper/TestTableMapper.xml b/src/main/resources/mapper/TestTableMapper.xml deleted file mode 100644 index f263e5c..0000000 --- a/src/main/resources/mapper/TestTableMapper.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - id,id_name - - diff --git a/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java b/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java index 174a528..3a07a7a 100644 --- a/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java +++ b/src/test/java/com/guo/learningprogresstracker/DataSourceTest.java @@ -1,8 +1,8 @@ package com.guo.learningprogresstracker; import cn.dev33.satoken.stp.StpUtil; -import com.guo.learningprogresstracker.entity.TestTableEntity; -import com.guo.learningprogresstracker.service.impl.TestTableServiceImpl; +import com.guo.learningprogresstracker.entity.TaskEntity; +import com.guo.learningprogresstracker.service.impl.TasksServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -13,39 +13,42 @@ import java.util.HashMap; import static org.junit.jupiter.api.Assertions.assertTrue; /** - * 用于测试数据库连接是否成功 + * 数据库连通性冒烟测试:用真实业务实体走一遍写入与删除,确认数据源与租户拦截器都正常。 + * + *

原先借测试专用表与测试控制器完成,随清理-01 一并改用真实实体, + * 避免为了冒烟测试在生产代码里保留一个无效端点与一张无用表。

*/ @SpringBootTest public class DataSourceTest { @Autowired - TestTableServiceImpl testTableService; + private TasksServiceImpl tasksServiceImpl; @BeforeEach void setUp() { StpUtil.login("1", "test_driver"); } - @Test - public void testAdd(){ - TestTableEntity testTableEntity = new TestTableEntity(); - testTableEntity.setIdName("guo_test"); - boolean save = testTableService.save(testTableEntity); - - assertTrue(save,"创建idName为guo_test的数据成功"); + private static TaskEntity newTask(String taskName) { + TaskEntity task = new TaskEntity(); + task.setTaskNum("TASK-SMOKE-" + System.nanoTime()); + task.setTaskName(taskName); + return task; } @Test - public void testDelete(){ - TestTableEntity testTableEntity = new TestTableEntity(); - testTableEntity.setIdName("guo_test_delete"); - testTableService.save(testTableEntity); + public void testAdd() { + TaskEntity task = newTask("guo_test"); + assertTrue(tasksServiceImpl.save(task), "写入学习任务应成功"); + } - HashMap stringStringHashMap = new HashMap<>(); - stringStringHashMap.put("id_name", "guo_test_delete"); - boolean delete = testTableService.removeByMap(stringStringHashMap); - - assertTrue(delete,"删除idName为guo_test的数据失败"); + @Test + public void testDelete() { + TaskEntity task = newTask("guo_test_delete"); + tasksServiceImpl.save(task); + HashMap condition = new HashMap<>(); + condition.put("task_num", task.getTaskNum()); + assertTrue(tasksServiceImpl.removeByMap(condition), "删除学习任务应成功"); } } diff --git a/src/test/java/com/guo/learningprogresstracker/controller/TestControllerUnitTest.java b/src/test/java/com/guo/learningprogresstracker/controller/TestControllerUnitTest.java deleted file mode 100644 index f06d744..0000000 --- a/src/test/java/com/guo/learningprogresstracker/controller/TestControllerUnitTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.guo.learningprogresstracker.controller; - -import com.guo.learningprogresstracker.entity.CommonResult; -import com.guo.learningprogresstracker.entity.TestTableEntity; -import com.guo.learningprogresstracker.exception.AppException; -import com.guo.learningprogresstracker.exception.ErrorParameterException; -import com.guo.learningprogresstracker.exception.NotFindEntitiesException; -import com.guo.learningprogresstracker.exception.OperationFailedException; -import com.guo.learningprogresstracker.service.TestTableService; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.when; - -/** - * TestController 纯单元测试:同包直接注入 mock 的 TestTableService 字段, - * 覆盖按 id 触发的各业务异常分支。 - */ -@ExtendWith(MockitoExtension.class) -class TestControllerUnitTest { - - @Mock - private TestTableService testTableService; - - @Test - void getTestEntityById_id1_throwsAppException() { - TestController controller = new TestController(); - controller.testTableService = testTableService; - - AppException ex = assertThrows(AppException.class, () -> controller.getTestEntityById("1")); - - assertEquals("测试AppException", ex.getMessage()); - verifyNoInteractions(testTableService); - } - - @Test - void getTestEntityById_id2_throwsErrorParameterException() { - TestController controller = new TestController(); - controller.testTableService = testTableService; - - ErrorParameterException ex = assertThrows(ErrorParameterException.class, - () -> controller.getTestEntityById("2")); - - assertEquals("测试ErrorParameterException", ex.getMessage()); - verifyNoInteractions(testTableService); - } - - @Test - void getTestEntityById_id3_throwsNotFindEntitiesException() { - TestController controller = new TestController(); - controller.testTableService = testTableService; - - NotFindEntitiesException ex = assertThrows(NotFindEntitiesException.class, - () -> controller.getTestEntityById("3")); - - assertEquals("测试NotFindEntitiesException", ex.getMessage()); - verifyNoInteractions(testTableService); - } - - @Test - void getTestEntityById_id4_throwsOperationFailedException() { - TestController controller = new TestController(); - controller.testTableService = testTableService; - - OperationFailedException ex = assertThrows(OperationFailedException.class, - () -> controller.getTestEntityById("4")); - - assertEquals("测试OperationFailedException", ex.getMessage()); - verifyNoInteractions(testTableService); - } - - @Test - void getTestEntityById_otherId_returnsEntityFromService() throws Exception { - TestController controller = new TestController(); - controller.testTableService = testTableService; - TestTableEntity entity = new TestTableEntity(); - when(testTableService.getTestEntityById("5")).thenReturn(entity); - - CommonResult result = controller.getTestEntityById("5"); - - assertEquals(200, result.getCode()); - assertSame(entity, result.getData()); - } -} diff --git a/src/test/java/com/guo/learningprogresstracker/entity/TestTableEntityTest.java b/src/test/java/com/guo/learningprogresstracker/entity/TestTableEntityTest.java deleted file mode 100644 index 8f78198..0000000 --- a/src/test/java/com/guo/learningprogresstracker/entity/TestTableEntityTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.guo.learningprogresstracker.entity; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * TestTableEntity 单元测试:覆盖手写的 equals/hashCode/toString 全部分支。 - */ -class TestTableEntityTest { - - private TestTableEntity entity(Integer id, String idName) { - TestTableEntity entity = new TestTableEntity(); - entity.setId(id); - entity.setIdName(idName); - return entity; - } - - @Test - void equals_sameReference_shouldBeTrue() { - TestTableEntity entity = entity(1, "a"); - assertEquals(entity, entity); - } - - @Test - void equals_null_shouldBeFalse() { - assertNotEquals(entity(1, "a"), null); - } - - @Test - void equals_differentClass_shouldBeFalse() { - TestTableEntity entity = entity(1, "a"); - TestTableEntity subclass = new TestTableEntity() { - }; - subclass.setId(1); - subclass.setIdName("a"); - assertNotEquals(entity, subclass); - } - - @Test - void equals_sameFields_shouldBeTrue() { - assertEquals(entity(1, "a"), entity(1, "a")); - } - - @Test - void equals_bothFieldsNull_shouldBeTrue() { - assertEquals(entity(null, null), entity(null, null)); - } - - @Test - void equals_differentId_shouldBeFalse() { - assertNotEquals(entity(1, "a"), entity(2, "a")); - } - - @Test - void equals_differentIdName_shouldBeFalse() { - assertNotEquals(entity(1, "a"), entity(1, "b")); - } - - @Test - void equals_idNullVsSet_shouldBeFalse() { - assertNotEquals(entity(null, "a"), entity(1, "a")); - } - - @Test - void equals_idNameNullVsSet_shouldBeFalse() { - assertNotEquals(entity(1, null), entity(1, "a")); - } - - @Test - void hashCode_equalEntities_shouldMatch() { - assertEquals(entity(1, "a").hashCode(), entity(1, "a").hashCode()); - } - - @Test - void hashCode_nullFields_shouldBeStable() { - assertEquals(entity(null, null).hashCode(), entity(null, null).hashCode()); - } - - @Test - void toString_shouldContainFields() { - TestTableEntity entity = entity(1, "测试"); - String text = entity.toString(); - - assertTrue(text.contains("TestTableEntity")); - assertTrue(text.contains("id=1")); - assertTrue(text.contains("idName=测试")); - assertTrue(text.contains("serialVersionUID=1")); - } -} diff --git a/src/test/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImplTest.java b/src/test/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImplTest.java deleted file mode 100644 index c653249..0000000 --- a/src/test/java/com/guo/learningprogresstracker/service/impl/TestTableServiceImplTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.guo.learningprogresstracker.service.impl; - -import com.guo.learningprogresstracker.entity.TestTableEntity; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; - -/** - * TestTableServiceImpl 单元测试:服务仅一个透传查询,覆盖 getById 透传路径。 - */ -@ExtendWith(MockitoExtension.class) -class TestTableServiceImplTest { - - @Spy - @InjectMocks - private TestTableServiceImpl testTableService; - - @Test - void getTestEntityById_shouldReturnMapperResult() { - TestTableEntity entity = new TestTableEntity(); - entity.setId(1); - entity.setIdName("测试记录"); - doReturn(entity).when(testTableService).getById(any()); - - TestTableEntity result = testTableService.getTestEntityById("1"); - - assertSame(entity, result); - assertEquals("测试记录", result.getIdName()); - } -}