diff --git a/backend-java/src/test/java/com/nanri/aiimage/config/TaskServiceWiringSliceTest.java b/backend-java/src/test/java/com/nanri/aiimage/config/TaskServiceWiringSliceTest.java new file mode 100644 index 000000000..0d4dd0251 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/config/TaskServiceWiringSliceTest.java @@ -0,0 +1,306 @@ +package com.nanri.aiimage.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.common.service.ResultDownloadResolver; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +import com.nanri.aiimage.modules.pricetrack.mapper.PriceTrackShopCandidateMapper; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackExcelAssemblyService; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackLoopRunService; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackTaskCacheService; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackTaskService; +import com.nanri.aiimage.modules.shopkey.mapper.QueryAsinMapper; +import com.nanri.aiimage.modules.shopkey.service.SkipPriceAsinService; +import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchShopCandidateMapper; +import com.nanri.aiimage.modules.shopmatch.service.ShopMatchExcelAssemblyService; +import com.nanri.aiimage.modules.shopmatch.service.ShopMatchTaskCacheService; +import com.nanri.aiimage.modules.shopmatch.service.ShopMatchTaskService; +import com.nanri.aiimage.modules.task.mapper.FileResultMapper; +import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; +import com.nanri.aiimage.modules.task.service.TaskDistributedLockService; +import com.nanri.aiimage.modules.task.service.TaskFileJobService; +import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler; +import com.nanri.aiimage.modules.task.service.TaskResultPayloadService; +import com.nanri.aiimage.modules.task.service.pipeline.AbstractShopPayloadTaskPipeline; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +import java.lang.reflect.Field; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * 迁移过的那几支 {@code *TaskService} 的**真实容器装配切片**(重构蓝图 Phase 3·用户 2026-10-19 拍板补的一格)。 + * + *
它替谁补位。Phase 2/3 连续两格把「真机启动验收」如实登记成未做,理由写在蓝图 §5: + * 本仓没有全容器装配测试({@code ConfigWiringSmokeTest} 只起 config 切片、 + * {@code ApiGuardCoverage*} 只起 HandlerMapping),而 {@code mvn -o spring-boot:run} 要 MySQL+Redis。 + * Phase 3 动了 {@code @Service} 的**构造器形状**(Lombok {@code @RequiredArgsConstructor} 生成枚 + * → 手写的显式枚,还要把 5 枚交给 {@code super})——这正是「装配相邻」的改动,光靠单测说不过去。 + * 本切片不连数据库,但走的是**真的 Spring 构造器解析**:隐式单构造器注入是否成立、 + * 底座那 5 枚是否真从 {@code super(...)} 落进来、两支是否各自拿到自己的缓存服务与装配服务。 + * + *
为什么不只断言「refresh() 没抛异常」。那是一句会说谎的判据:Spring 6 在没有 + * {@code @Autowired} 而存在多枚 public 构造器时,会**静默挑无参那一枚**,容器照样起得来、 + * 字段全 null。所以下面用两枚探针类各做一次非零对照(一枚该红、一枚「起得来但字段空」), + * 再用逐枚 {@code assertSame} 的字段断言作为真正的判据——探针不红、字段不空,这两条同时成立 + * 才说明这套断言有牙。 + * + *
依赖一律是 Mockito 桩:本切片量的是**装配**,不是这些协作者的行为。
+ */
+class TaskServiceWiringSliceTest {
+
+ @Configuration
+ static class Collaborators {
+ @Bean FileTaskMapper fileTaskMapper() {
+ return mock(FileTaskMapper.class);
+ }
+
+ @Bean FileResultMapper fileResultMapper() {
+ return mock(FileResultMapper.class);
+ }
+
+ @Bean ShopMatchShopCandidateMapper shopMatchCandidateMapper() {
+ return mock(ShopMatchShopCandidateMapper.class);
+ }
+
+ @Bean PriceTrackShopCandidateMapper priceTrackCandidateMapper() {
+ return mock(PriceTrackShopCandidateMapper.class);
+ }
+
+ @Bean ZiniaoShopSwitchService ziniaoShopSwitchService() {
+ return mock(ZiniaoShopSwitchService.class);
+ }
+
+ @Bean ShopMatchExcelAssemblyService shopMatchExcelAssemblyService() {
+ return mock(ShopMatchExcelAssemblyService.class);
+ }
+
+ @Bean PriceTrackExcelAssemblyService priceTrackExcelAssemblyService() {
+ return mock(PriceTrackExcelAssemblyService.class);
+ }
+
+ @Bean OssStorageService ossStorageService() {
+ return mock(OssStorageService.class);
+ }
+
+ @Bean ResultDownloadResolver resultDownloadResolver() {
+ return mock(ResultDownloadResolver.class);
+ }
+
+ @Bean ObjectMapper objectMapper() {
+ return mock(ObjectMapper.class);
+ }
+
+ @Bean ShopMatchTaskCacheService shopMatchTaskCacheService() {
+ return mock(ShopMatchTaskCacheService.class);
+ }
+
+ @Bean PriceTrackTaskCacheService priceTrackTaskCacheService() {
+ return mock(PriceTrackTaskCacheService.class);
+ }
+
+ @Bean com.nanri.aiimage.config.TaskPressureProperties taskPressureProperties() {
+ return mock(com.nanri.aiimage.config.TaskPressureProperties.class);
+ }
+
+ @Bean TaskResultPayloadService taskResultPayloadService() {
+ return mock(TaskResultPayloadService.class);
+ }
+
+ @Bean TaskFileJobService taskFileJobService() {
+ return mock(TaskFileJobService.class);
+ }
+
+ @Bean TaskDistributedLockService taskDistributedLockService() {
+ return mock(TaskDistributedLockService.class);
+ }
+
+ @Bean SkipPriceAsinService skipPriceAsinService() {
+ return mock(SkipPriceAsinService.class);
+ }
+
+ @Bean QueryAsinMapper queryAsinMapper() {
+ return mock(QueryAsinMapper.class);
+ }
+
+ @Bean TaskProgressLightAssembler taskProgressLightAssembler() {
+ return mock(TaskProgressLightAssembler.class);
+ }
+
+ @Bean PlatformTransactionManager platformTransactionManager() {
+ return mock(PlatformTransactionManager.class);
+ }
+
+ @Bean PriceTrackLoopRunService priceTrackLoopRunService() {
+ return mock(PriceTrackLoopRunService.class);
+ }
+
+ @Bean LocalFileStorageService localFileStorageService() {
+ return mock(LocalFileStorageService.class);
+ }
+ }
+
+ private static AnnotationConfigApplicationContext context(Class>... services) {
+ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
+ ctx.register(Collaborators.class);
+ ctx.register(services);
+ ctx.refresh();
+ return ctx;
+ }
+
+ /** 沿类继承链找字段——底座那 5 枚在 {@code AbstractFileTaskPipeline} 上,本支的在自己的类里。 */
+ private static Object fieldValue(Object target, String fieldName) {
+ for (Class> type = target.getClass(); type != null; type = type.getSuperclass()) {
+ try {
+ Field field = type.getDeclaredField(fieldName);
+ field.setAccessible(true);
+ return field.get(target);
+ } catch (NoSuchFieldException ignored) {
+ // 继续往父类找;全链都没有才算缺件
+ } catch (IllegalAccessException ex) {
+ throw new IllegalStateException("读不到 " + target.getClass() + "." + fieldName, ex);
+ }
+ }
+ throw new AssertionError("字段不存在:" + target.getClass().getName() + "#" + fieldName
+ + "(迁移把字段沉进底座时改名了?那就该红,别让它悄悄通过)");
+ }
+
+ // ------------------------------------------------------------------
+ // 正判:真实容器把两支建起来,且每一枚依赖都落在它该落的位置
+ // ------------------------------------------------------------------
+
+ @Test
+ void migratedServicesInstantiateThroughTheSpringContainer() {
+ try (AnnotationConfigApplicationContext ctx =
+ context(ShopMatchTaskService.class, PriceTrackTaskService.class)) {
+ ShopMatchTaskService shopMatch = ctx.getBean(ShopMatchTaskService.class);
+ PriceTrackTaskService priceTrack = ctx.getBean(PriceTrackTaskService.class);
+
+ assertNotNull(shopMatch, "Lombok 生成枚换成显式枚之后,Spring 仍要能隐式构造注入");
+ assertNotNull(priceTrack);
+ assertTrue(shopMatch instanceof AbstractShopPayloadTaskPipeline,
+ "Phase 3 的判据是「真有生产调用方继承底座」,不是只由单测证明契约可用");
+ assertTrue(priceTrack instanceof AbstractShopPayloadTaskPipeline);
+ }
+ }
+
+ @Test
+ void superCallLandsTheFiveBaseDependenciesInOrder() {
+ try (AnnotationConfigApplicationContext ctx =
+ context(ShopMatchTaskService.class, PriceTrackTaskService.class)) {
+ ShopMatchTaskService shopMatch = ctx.getBean(ShopMatchTaskService.class);
+ PriceTrackTaskService priceTrack = ctx.getBean(PriceTrackTaskService.class);
+
+ // 这五枚是交给 super(...) 的那五枚。参数表里它们与其余 12/13 枚同类型不重复,
+ // 换位会在编译期就被挡住——但「少交一枚」「交 null」不会。逐枚 assertSame 才是硬证。
+ for (Object service : List.of(shopMatch, priceTrack)) {
+ assertSame(ctx.getBean(FileTaskMapper.class), fieldValue(service, "fileTaskMapper"));
+ assertSame(ctx.getBean(FileResultMapper.class), fieldValue(service, "fileResultMapper"));
+ assertSame(ctx.getBean(TaskDistributedLockService.class),
+ fieldValue(service, "taskDistributedLockService"));
+ assertSame(ctx.getBean(TaskFileJobService.class), fieldValue(service, "taskFileJobService"));
+ assertSame(ctx.getBean(OssStorageService.class), fieldValue(service, "ossStorageService"));
+ }
+
+ // 两支各自的私有件:绝不能把 A 支的缓存服务注进 B 支(同族两个 bean,最容易串的就是这儿)
+ assertSame(ctx.getBean(ShopMatchTaskCacheService.class), fieldValue(shopMatch, "shopMatchTaskCacheService"));
+ assertSame(ctx.getBean(PriceTrackTaskCacheService.class), fieldValue(priceTrack, "priceTrackTaskCacheService"));
+ assertSame(ctx.getBean(ShopMatchExcelAssemblyService.class), fieldValue(shopMatch, "excelAssemblyService"));
+ assertSame(ctx.getBean(PriceTrackExcelAssemblyService.class), fieldValue(priceTrack, "excelAssemblyService"));
+ assertSame(ctx.getBean(ShopMatchShopCandidateMapper.class), fieldValue(shopMatch, "candidateMapper"));
+ assertSame(ctx.getBean(PriceTrackShopCandidateMapper.class), fieldValue(priceTrack, "candidateMapper"));
+ assertSame(ctx.getBean(PriceTrackLoopRunService.class), fieldValue(priceTrack, "priceTrackLoopRunService"));
+ assertSame(ctx.getBean(LocalFileStorageService.class), fieldValue(priceTrack, "localFileStorageService"));
+ assertSame(ctx.getBean(QueryAsinMapper.class), fieldValue(shopMatch, "queryAsinMapper"));
+ assertSame(ctx.getBean(PlatformTransactionManager.class), fieldValue(shopMatch, "transactionManager"));
+ assertSame(ctx.getBean(PlatformTransactionManager.class), fieldValue(priceTrack, "transactionManager"));
+ }
+ }
+
+ @Test
+ void injectedCollaboratorsAreActuallyUsedAndEachBranchReportsItsOwnModuleType() {
+ try (AnnotationConfigApplicationContext ctx =
+ context(ShopMatchTaskService.class, PriceTrackTaskService.class)) {
+ TaskProgressLightAssembler assembler = ctx.getBean(TaskProgressLightAssembler.class);
+ ShopMatchTaskService shopMatch = ctx.getBean(ShopMatchTaskService.class);
+ PriceTrackTaskService priceTrack = ctx.getBean(PriceTrackTaskService.class);
+ List