时间:2025-06-29 21:53
人气:
作者:admin
我们Spring AI工程引用代码如下,由于基于Mock单元测试下ChatClient总是为空,异常是
Cannot invoke "org.springframework.ai.chat.client.ChatClient.prompt(String)" because "this.chatClient" is null
Spring AI 1.0.0的正式发布时间为2025年5月20日
@Service
public class TaskMcpCallServerServices implements AiService, ChatService {
private final ChatClient chatClient;
/**
* Constructs a new TaskMcpCallServerServices instance.
* @param aiClientBuilder the ChatClient builder used to create the chat client
*/
public TaskMcpCallServerServices(ChatClient.Builder chatClientBuilder) {
this.chatClient = chatClientBuilder.build();
}
@Override
public ChatClient getChatClient() {
return chatClient;
}
@Override
/**
* Gets AI response for the given prompt by calling the MCP server.
* @param prompt the input prompt to send to the AI
* @return ResponseEntity containing the AI response
*/
public ResponseEntity<String> getAiResponse(String prompt) {
String response = this.chatClient
.prompt(prompt)
.call()
.content();
return ResponseEntity.ok(response);
}
}
@ExtendWith(MockitoExtension.class)
class TaskMcpCallServerServicesTest {
@Mock
private ChatClient.Builder chatClientBuilder;
@Mock
private ChatClient chatClient;
@InjectMocks
private TaskMcpCallServerServices taskMcpCallServerServices;
@Test
void testGetAiResponse() {
// Given
String testPrompt = "test prompt";
String expectedResponse = "test response";
when(chatClientBuilder.build()).thenReturn(chatClient);
// When
ResponseEntity<String> actualResponse = taskMcpCallServerServices.getAiResponse(testPrompt);
// Then
assertEquals(expectedResponse, actualResponse.getBody());
}
}
看上去解决问题思路没有问题,但实际还是不能解决ChatClient空指针问题。
我们查看修改后代码,并不理想
@RestController
@RequestMapping("/tools")
public class ToolController {
private final AiService aiService;
public ToolController(AiService aiService, ChatService chatService) {
this.aiService = aiService;
}
/**
* 使用阿里云对话服务(AMAP)进行文本对话
*
* @param prompt 用户输入的对话内容
* @return ResponseEntity 包含对话结果的HTTP响应
*/
@GetMapping("")
public ResponseEntity<String> amap(String prompt) {
return aiService.getAiResponse(prompt);
}
}
存在如下问题,大模型的知识还是跟不上组件更新
java: 找不到符号
符号: 类 CallPromptSpec
位置: 接口 org.springframework.ai.chat.client.ChatClient
请对当前类引用类型ChatClient进行完全隔离,ChatClient来自Spring AI框架,我们期望隔离它,并且可以替换为其他AI类库的实现,请基于OOP编程原则修改相关关联代码。
单元测试没有修复,第二轮对话修复单元测试问题
最终Qwen3达到期望的结果
/**
* 聊天客户端适配器接口
* 提供统一的方法调用规范,可扩展支持不同AI框架的实现
*/
public interface ChatAdapter {
/**
* 执行AI对话请求
* @param prompt 用户输入内容
* @return 返回处理结果
*/
String chat(String prompt);
}
/**
* Spring AI框架适配器实现
*/
@Component
public class SpringAiChatAdapter implements ChatAdapter {
private final ChatClient chatClient;
public SpringAiChatAdapter(ChatClient chatClient) {
this.chatClient = chatClient;
}
@Override
public String chat(String prompt) {
return chatClient.prompt(prompt).call().content();
}
}
@Service
public class TaskMcpCallServerServices implements AiService {
private final ChatAdapter chatAdapter;
/**
* Constructs a new TaskMcpCallServerServices instance.
* @param chatAdapter the ChatAdapter instance to use
*/
public TaskMcpCallServerServices(ChatAdapter chatAdapter) {
this.chatAdapter = chatAdapter;
}
@Override
/**
* Gets AI response for the given prompt by calling the MCP server.
* @param prompt the input prompt to send to the AI
* @return ResponseEntity containing the AI response
*/
public ResponseEntity<String> getAiResponse(String prompt) {
String response = this.chatAdapter.chat(prompt);
return ResponseEntity.ok(response);
}
}
单元测试代码
@ExtendWith(MockitoExtension.class)
class TaskMcpCallServerServicesTest {
@Mock
private ChatAdapter chatAdapter;
@InjectMocks
private TaskMcpCallServerServices taskMcpCallServerServices;
@BeforeEach
public void before()
{
MockitoAnnotations.openMocks(this);
taskMcpCallServerServices = new TaskMcpCallServerServices(chatAdapter);
}
@Test
void testGetAiResponse() {
// Given
String testPrompt = "test prompt";
String expectedResponse = "test response";
when(chatAdapter.chat(testPrompt)).thenReturn(expectedResponse);
// When
ResponseEntity<String> actualResponse = taskMcpCallServerServices.getAiResponse(testPrompt);
// Then
assertEquals(expectedResponse, actualResponse.getBody());
}
}
新生成文件,编译不通过,并且多次修复未果
存在单元测试问题,第二轮修复单元测试成功
/**
* Interface defining the contract for AI client implementations.
* Provides abstraction for different AI service providers.
*/
public interface AiClient {
/**
* Gets AI-generated response for the given prompt.
* @param prompt the input text to send to the AI service
* @return the AI response content
*/
String getResponse(String prompt);
}
/**
* Service class for handling MCP server calls and AI responses.
* Implements AiService interface to provide AI response generation functionality.
*/
@Service
public class TaskMcpCallServerServices implements AiService {
private final AiClient aiClient;
/**
* Constructs a new TaskMcpCallServerServices instance.
* @param aiClient the AI client implementation
*/
public TaskMcpCallServerServices(AiClient aiClient) {
this.aiClient = aiClient;
}
@Override
/**
* Gets AI response for the given prompt by calling the MCP server.
* @param prompt the input prompt to send to the AI
* @return ResponseEntity containing the AI response
*/
public ResponseEntity<String> getAiResponse(String prompt) {
String response = this.aiClient.getResponse(prompt);
return ResponseEntity.ok(response);
}
}
重构基本是成功的,还使用简单工厂,但新生成UnitTest依赖的API存在版本问题,最终效果如下
java.util.Date到java.time包的迁移,涉及SimpleDateFormat到DateTimeFormatter的替换,但AI可能因训练数据滞后继续推荐旧API。
collections.abc模块的调整,导致直接继承collections.MutableSequence的类需要修改导入路径。queue.Queue而忽略项目已依赖的第三方库(如deque的线程安全封装)。
最终,OOP与AI的融合将走向自适应软件系统:
这一进程要求开发者从“代码编写者”转型为“系统架构师+AI训练师”,在OOP的抽象层次与AI的生成能力之间构建新的认知桥梁。