如何使用Spring上下文在Spring-Junit中模拟来自私有@PostConstruct的外部方法调用?

时间:2019-07-03 17:50:20

标签: java spring spring-boot junit

我正在Spring Boot项目中研究服务的JUnit测试用例。我需要介绍私有的@PostConstruct方法,该方法具有一个REST调用,并且需要模拟,否则测试将失败。有什么办法可以不更改代码?示例服务如下:

    @Service
    public class SampleService {

        @Autowired
        private RestTemplate restTemplate;

        private List<String> users;

        @PostConstruct
        private void init() {
            users = restTemplate.getForObject("url", List.class);
        }
    }

0 个答案:

没有答案