我正在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);
}
}