You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未使用@SpringBootTest创建@RestClientTest测试时出现找不到@SpringBootConfiguration报错

问题:使用@RestClientTest测试RestTemplate依赖类时找不到SpringBootConfiguration

我要给依赖RestTemplate进行REST调用的SOClient类编写单元测试,类代码如下:

public class SOClient { 

    private final RestTemplate restTemplate;
    ... // 构造方法等
    public Questions getQuestions() {
        ResponseEntity<Questions[]> entity = restTemplate.getForEntity("http://some-api.com", Questions[].class);
        ...
    } 

参考教程编写了测试类:

@RunWith(SpringRunner.class)
@RestClientTest({SOClient.class})
public class SOClientTest {
 
   public void test(){}
}

但运行时出现错误:

java.lang.IllegalStateException: Unable to find a
@SpringBootConfiguration, you need to use @ContextConfiguration or
@SpringBootTest(classes=...) with your test

我不想添加@SpringBootTest注解,因为不想加载数据库层等无关组件,只想搭建测试所需的最小Spring上下文。


解决方案

方法一:创建独立测试配置类并通过@ContextConfiguration引入

创建仅包含测试必需Bean的配置类,避免加载完整应用上下文:

@Configuration
public class TestConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public SOClient soClient(RestTemplate restTemplate) {
        return new SOClient(restTemplate);
    }
}

修改测试类,指定配置来源并添加测试逻辑:

@RunWith(SpringRunner.class)
@RestClientTest
@ContextConfiguration(classes = TestConfig.class)
public class SOClientTest {

    @Autowired
    private SOClient soClient;

    @Autowired
    private MockRestServiceServer mockServer;

    @Test
    public void testGetQuestions() {
        // 模拟目标API的响应数据
        Questions[] mockQuestions = {new Questions("测试问题")};
        
        // 配置MockServer,匹配请求URL并返回模拟响应
        mockServer.expect(requestTo("http://some-api.com"))
                .andRespond(withSuccess(mockQuestions, MediaType.APPLICATION_JSON));

        // 执行待测试方法
        Questions result = soClient.getQuestions();

        // 验证请求是否被正确触发
        mockServer.verify();
        // 添加其他断言逻辑,比如验证结果非空、数据匹配等
        assertNotNull(result);
    }
}

方法二:在测试类内部定义静态配置类

如果不想单独创建配置文件,可以将配置类作为测试类的静态内部类,同样通过@ContextConfiguration指定:

@RunWith(SpringRunner.class)
@RestClientTest
@ContextConfiguration
public class SOClientTest {

    @Configuration
    static class TestConfig {
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }

        @Bean
        public SOClient soClient(RestTemplate restTemplate) {
            return new SOClient(restTemplate);
        }
    }

    @Autowired
    private SOClient soClient;

    @Autowired
    private MockRestServiceServer mockServer;

    @Test
    public void testGetQuestions() {
        // 测试逻辑同方法一
    }
}

核心说明

@RestClientTest会自动配置MockRestServiceServer等测试专用Bean,但需要明确指定Spring配置的来源。通过@ContextConfiguration引入仅包含测试必要Bean的最小配置类,既满足测试所需的环境,又避免加载数据库等无关组件,实现轻量级单元测试。


内容的提问来源于stack exchange,提问作者fascynacja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 14:02:36