如何在 Spring Boot 中将 Mockito 用于 restTemplateBuilder
How to used Mockito for restTemplateBuilder in Spring Boot
我有一个 spring 休息消费者如下。
在这个服务中,我们需要使用另一个自动装配的服务。
如何模拟 restTemplate
的响应?
@Service
public class TestConsumer {
private final RestTemplate restTemplate;
@Autowired
private AccountService accountService;
public TestConsumer(RestTemplateBuilder restTemplateBuilder) {
restTemplate = restTemplateBuilder.basicAuthentication("username", "password")
.rootUri("https://example.com").build();
}
public String testGet() {
Account account = accountService.getAccount();
String r = "";
if("Admin".equals(account.getType())) {
r = restTemplate.getForObject("AAA", String.class);
}else {
r = restTemplate.getForObject("BBB", String.class);
}
return r;
}
}
我建议您也将 AccountService
作为构造函数参数,以便更轻松地测试您的 class:
@Service
public class TestConsumer {
private final RestTemplate restTemplate;
private AccountService accountService;
public TestConsumer(RestTemplateBuilder restTemplateBuilder, AccountService accountService) {
restTemplate = restTemplateBuilder.basicAuthentication("username", "password")
.rootUri("https://example.com").build();
this.accountService = accountService;
}
public String testGet() {
Account account = accountService.getAccount();
String r = "";
if("Admin".equals(account.getType())) {
r = restTemplate.getForObject("AAA", String.class);
}else {
r = restTemplate.getForObject("BBB", String.class);
}
return r;
}
}
然后我会模拟 RestTemplateBuilder
和 RestTemplate
如下:
public class TestConsumerTest {
@Mock
private RestTemplate restTemplate;
@Mock
private RestTemplateBuilder restTemplateBuilder;
@Mock
private AccountService accountService;
private TestConsumer testConsumer;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
// Mock RestTemplateBuilder behaviour
doReturn(this.restTemplateBuilder).when(this.restTemplateBuilder).basicAuthentication(anyString(), anyString());
doReturn(this.restTemplateBuilder).when(this.restTemplateBuilder).rootUri(anyString());
doReturn(this.restTemplate).when(this.restTemplateBuilder).build();
// Mock RestTemplate behaviour
doReturn("whatever-string").when(this.restTemplate).getForObject(eq("AAA"), eq(String.class));
doReturn("whatever-another-string").when(this.restTemplate).getForObject(eq("BBB"), eq(String.class));
this.testConsumer = new TestConsumer(restTemplateBuilder, accountService);
}
// Your tests
}
我有一个 spring 休息消费者如下。
在这个服务中,我们需要使用另一个自动装配的服务。
如何模拟 restTemplate
的响应?
@Service
public class TestConsumer {
private final RestTemplate restTemplate;
@Autowired
private AccountService accountService;
public TestConsumer(RestTemplateBuilder restTemplateBuilder) {
restTemplate = restTemplateBuilder.basicAuthentication("username", "password")
.rootUri("https://example.com").build();
}
public String testGet() {
Account account = accountService.getAccount();
String r = "";
if("Admin".equals(account.getType())) {
r = restTemplate.getForObject("AAA", String.class);
}else {
r = restTemplate.getForObject("BBB", String.class);
}
return r;
}
}
我建议您也将 AccountService
作为构造函数参数,以便更轻松地测试您的 class:
@Service
public class TestConsumer {
private final RestTemplate restTemplate;
private AccountService accountService;
public TestConsumer(RestTemplateBuilder restTemplateBuilder, AccountService accountService) {
restTemplate = restTemplateBuilder.basicAuthentication("username", "password")
.rootUri("https://example.com").build();
this.accountService = accountService;
}
public String testGet() {
Account account = accountService.getAccount();
String r = "";
if("Admin".equals(account.getType())) {
r = restTemplate.getForObject("AAA", String.class);
}else {
r = restTemplate.getForObject("BBB", String.class);
}
return r;
}
}
然后我会模拟 RestTemplateBuilder
和 RestTemplate
如下:
public class TestConsumerTest {
@Mock
private RestTemplate restTemplate;
@Mock
private RestTemplateBuilder restTemplateBuilder;
@Mock
private AccountService accountService;
private TestConsumer testConsumer;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
// Mock RestTemplateBuilder behaviour
doReturn(this.restTemplateBuilder).when(this.restTemplateBuilder).basicAuthentication(anyString(), anyString());
doReturn(this.restTemplateBuilder).when(this.restTemplateBuilder).rootUri(anyString());
doReturn(this.restTemplate).when(this.restTemplateBuilder).build();
// Mock RestTemplate behaviour
doReturn("whatever-string").when(this.restTemplate).getForObject(eq("AAA"), eq(String.class));
doReturn("whatever-another-string").when(this.restTemplate).getForObject(eq("BBB"), eq(String.class));
this.testConsumer = new TestConsumer(restTemplateBuilder, accountService);
}
// Your tests
}