Jelajahi Sumber

test(video): 接线测试改为断言注解过滤的双包扫描

Kazerin 3 minggu lalu
induk
melakukan
b5f8fe414e

+ 19 - 7
video-service/src/test/java/com/zksy/video/VideoApplicationWiringTest.java

@@ -4,29 +4,41 @@ import org.junit.jupiter.api.Test;
 import org.mybatis.spring.annotation.MapperScan;
 import java.util.Arrays;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
- * The video module registers its two mappers with @Mapper, so a package-wide MapperScan is both
- * unnecessary and harmful: it also turns ordinary interfaces (VideoLifecycleStore) into mapper
- * beans, which makes VideoLifecycleService ambiguous and prevents the service from starting.
+ * Declaring @MapperScan at all disables the MyBatis starter's automatic package scan, so the
+ * annotation must list both mapper packages. The filter is what keeps ordinary interfaces such as
+ * VideoLifecycleStore out: without it a package-wide scan registers them as mappers and makes
+ * VideoLifecycleService ambiguous, which prevents the service from starting.
  */
 class VideoApplicationWiringTest {
 
     @Test
-    void mapperScanDoesNotSweepTheWholeVideoPackage() {
+    void mapperScanCoversBothPackagesButOnlyAnnotatedInterfaces() {
         MapperScan scan = VideoApplication.class.getAnnotation(MapperScan.class);
         assertTrue(scan != null, "VideoApplication must declare its mapper scan explicitly");
-        assertFalse(Arrays.asList(scan.basePackages()).contains("com.zksy.video"),
-                "com.zksy.video must not be scanned for mappers: it holds ordinary interfaces such as VideoLifecycleStore");
+        // Both packages are required: declaring @MapperScan disables the starter's automatic scan.
+        assertTrue(Arrays.asList(scan.basePackages()).contains("com.zksy.video"),
+                "com.zksy.video mappers are not auto-scanned once @MapperScan is declared");
         assertTrue(Arrays.asList(scan.basePackages()).contains("com.ruoyi.wvp.mapper"),
                 "the wvp mappers still need the package scan");
+        assertEquals(org.apache.ibatis.annotations.Mapper.class, scan.annotationClass(),
+                "only @Mapper interfaces may be registered; a plain sweep breaks VideoLifecycleStore");
     }
 
     @Test
-    void bothVideoMappersDeclareThemselvesSoThePackageScanIsNotNeeded() {
+    void bothVideoMappersDeclareThemselvesSoTheSweepCanBeFiltered() {
         assertTrue(RegisteredVideoMapper.class.isAnnotationPresent(org.apache.ibatis.annotations.Mapper.class));
         assertTrue(VideoEquipmentStatusMapper.class.isAnnotationPresent(org.apache.ibatis.annotations.Mapper.class));
     }
+
+    @Test
+    void ordinaryLifecycleInterfacesAreNotMappers() {
+        assertFalse(com.zksy.video.lifecycle.VideoLifecycleStore.class
+                        .isAnnotationPresent(org.apache.ibatis.annotations.Mapper.class),
+                "VideoLifecycleStore is an ordinary interface; registering it as a mapper breaks injection");
+    }
 }