После того, как вы отделите конфигурацию от контроллера, как предложено @chrylis, вы продолжите в том же духе.
Вы должны пытаться смоделировать метод RequestEntity.post. Обратите внимание, что это статический метод, и он немного отличается от обычных методов экземпляра public. Для этого вам нужно использовать PowerMockito, так как Mockito не подойдет.
добавить зависимость в pom следующим образом:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
затем аннотируйте тестовый класс с помощью @RunWith
и @PrepareForTest
примерно так:
@RunWith(PowerMockRunner.class)
@PrepareForTest({RequestEntity.class})
public class TestClass {
}
и смоделируйте метод post следующим образом:
PowerMockito.mockStatic(RequestEntity.class); when(RequestEntity.post(any(URI.class))).thenReturn(getRequestEntityResponseBody());
private RequestEntity< CustomerInfo > getRequestEntityResponseBody(){
//code
}
UPDATE
CustomerInfo customerInfo = new CustomerInfo();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
RequestEntity<CustomerInfo> customerInfoRequestEntity = new ResponseEntity<CustomerInfo>(customerInfo, responseHeaders, HttpStatus.OK);
PowerMockito.mockStatic(RequestEntity.class);
when(RequestEntity.post(any(URI.class))).thenReturn(customerInfoRequestEntity);