Last active
October 6, 2020 20:48
-
-
Save justin-lyon/1c64017f4cb4cb9730213aec7905f159 to your computer and use it in GitHub Desktop.
Trying to test some stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class RestTemplateConfiguration { | |
public static final String REST_TEMPLATE_NAME = "someRestTemplate"; | |
@Autowired | |
private AppConfig appConfig; | |
@Value("${props.useProxy}) | |
private boolean useProxy; | |
@Bean(name = REST_TEMPLATE_NAME) | |
public RestTemplate restTemplate(RestTemplateBuilder builder) { | |
return builder | |
.rootUri("") | |
.requestFactory(this::getHttpClientFactory) // -> NPE Here | |
.interceptors(getInterceptors()) | |
.errorHandler(getErrorHandlers()) | |
.build() | |
} | |
@Bean | |
public HttpComponentsClientHttpRequestFactory getHttpClientFactory() { | |
HttpComponentsClientHttpRequestFactory reqFactory = new HttpComponentsClientHttpRequestFactory(); | |
// ...does some SSL / Proxy stuff | |
return reqFactory; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringJUnit4ClassRunner.class) | |
public class RestTemplateConfigurationTest { | |
@InjectMocks | |
private RestTemplateConfiguration templateConfig; | |
@Mock | |
@Qualifier(RestTemplateConfiguration.REST_TEMPLATE_NAME) | |
private RestTemplateBuilder builder; | |
@BeforeEach | |
public void setup() { | |
initMocks(this); | |
} | |
@Test | |
public void makeRequest_givenCustomHttpClientFactory_shouldRespondOK { | |
RestTemplate template = new RestTemplateBuilder() | |
.requestFactory(this.templateConfig::getHttpClientFactory) | |
.build() | |
ResponseEntity<String> response = null; | |
boolean caughtException = false; | |
try { | |
response = template.exchange("https://google.com", HttpMethod.GET, null, String.class); | |
} catch(Exception e) { | |
caughtException = true; | |
} | |
assertEquals(false, caughtException, "Should not have exception"); | |
assertEquals(HttpStatus.OK, response.getStatusCode(), "Status should be OK."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment