test(phase-3): 补真实容器装配切片——两支 @Service 的构造器形状改动从推理变成跑过
蓝图 §4 第 4 步那格「真机启动验收」连续两笔如实登记未做(本仓没有全容器装配测试, spring-boot:run 要 MySQL+Redis),Phase 3 又恰好动了 @Service 的构造器形状 (Lombok 生成枚 → 手写显式枚,还多一层 super(...) 交 5 枚)——装配相邻的风险不该只写在注释里。 本切片不连库,走的是**真的 Spring 构造器解析**(AnnotationConfigApplicationContext+@Bean 桩), 用户 2026-10-19 拍板补这一格。 三条正判:① 两支在容器里建得出来且真的 instanceof AbstractShopPayloadTaskPipeline (Phase 3 的准出是「有生产调用方继承底座」,不是单测证明契约可用); ② 交给 super 的那 5 枚 + 两支各自的 12/13 枚私有件**逐枚 assertSame** 到对应桩 bean ——同族两个缓存服务、两个装配服务、两个 candidateMapper 是最容易串位的, 少交一枚或交 null 编译期挡不住,只有这里看得见; ③ 调一次 progressLight 后 verify assembler 分别收到 "SHOP_MATCH" 与 "PRICE_TRACK" 一次: moduleType() 是迁移新加的钩子,回错一支会把心跳、结果行与组装 Job 全挂到别人模块上, 而建单照样成功——那种错只有这条能抓。 两条非零对照(缺了它们,上面的绿就说明不了任何事): - 两枚 public 构造器且都带参 ⇒ 容器必须起不来(探针实测起不来 ⇒ 判据不是恒绿); - 一枚无参+一枚带参 ⇒ Spring **静默挑无参那一枚**,refresh 成功而字段全 null。 第二条专门钉住「只断言 refresh 不抛异常」这种会说谎的判据,所以正判走的全是 assertSame/verify。 依赖一律 Mockito 桩:本片量的是装配,不是协作者行为。5 例全绿。
This commit is contained in:
@@ -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 拍板补的一格)。
|
||||
*
|
||||
* <p><b>它替谁补位。</b>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(...)} 落进来、两支是否各自拿到自己的缓存服务与装配服务。
|
||||
*
|
||||
* <p><b>为什么不只断言「refresh() 没抛异常」。</b>那是一句会说谎的判据:Spring 6 在没有
|
||||
* {@code @Autowired} 而存在多枚 public 构造器时,会**静默挑无参那一枚**,容器照样起得来、
|
||||
* 字段全 null。所以下面用两枚探针类各做一次非零对照(一枚该红、一枚「起得来但字段空」),
|
||||
* 再用逐枚 {@code assertSame} 的字段断言作为真正的判据——探针不红、字段不空,这两条同时成立
|
||||
* 才说明这套断言有牙。
|
||||
*
|
||||
* <p>依赖一律是 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<Long> taskIds = List.of(1L);
|
||||
|
||||
shopMatch.progressLight(taskIds, 7L);
|
||||
priceTrack.progressLight(taskIds, 7L);
|
||||
|
||||
// moduleType() 是迁移新加的钩子:回错一支,心跳、结果行与组装 Job 全挂到别人模块上,
|
||||
// 而建单照样成功——那种错只有在这里才看得见。
|
||||
verify(assembler, times(1)).assemble(eq("SHOP_MATCH"), eq(7L), eq(taskIds));
|
||||
verify(assembler, times(1)).assemble(eq("PRICE_TRACK"), eq(7L), eq(taskIds));
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 非零对照:证明上面那两条判据不是恒绿
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/** 两枚 public 构造器且都带参:Spring 找不到候选构造器 ⇒ 容器必须起不来。 */
|
||||
static class TwoConstructorsService {
|
||||
TwoConstructorsService(FileTaskMapper fileTaskMapper) {
|
||||
}
|
||||
|
||||
TwoConstructorsService(FileTaskMapper fileTaskMapper, FileResultMapper fileResultMapper) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 一枚无参+一枚带参:Spring **静默挑无参那一枚**,容器起得来、字段全 null——所以「起得来」不是证据。 */
|
||||
static class SilentNoArgConstructorService {
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
|
||||
SilentNoArgConstructorService() {
|
||||
this.fileTaskMapper = null;
|
||||
}
|
||||
|
||||
SilentNoArgConstructorService(FileTaskMapper fileTaskMapper) {
|
||||
this.fileTaskMapper = fileTaskMapper;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambiguousConstructorsFailTheContainerSoTheSliceCanActuallyGoRed() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
|
||||
ctx.register(Collaborators.class);
|
||||
ctx.register(TwoConstructorsService.class);
|
||||
assertThrows(Exception.class, ctx::refresh,
|
||||
"探针没红 ⇒ 这套装配判据是恒绿的,正判用例说了不算");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerStartingGreenIsNotProofBecauseSpringSilentlyPicksTheNoArgConstructor() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
|
||||
ctx.register(Collaborators.class);
|
||||
ctx.register(SilentNoArgConstructorService.class);
|
||||
ctx.refresh();
|
||||
SilentNoArgConstructorService probe = ctx.getBean(SilentNoArgConstructorService.class);
|
||||
|
||||
assertNotNull(probe, "无参那一枚被静默选中:容器确实起得来");
|
||||
assertNull(fieldValue(probe, "fileTaskMapper"),
|
||||
"而字段是空的——这正是「只断言 refresh 不抛异常」会放过去的失效模式,"
|
||||
+ "所以上面两条用例走的是逐枚 assertSame");
|
||||
verifyNoInteractions(ctx.getBean(FileTaskMapper.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user