We've got a Spring Boot 1.4 web app the exposes a REST api, and we want to run some integration tests.
In our test, we get Spring Boot to spin up the web app locally and then we make some calls against the api.
If we simply run the web app itself and hit the endpoint, we get 200/OK
response, which is expected. Running the test, however, we are getting a 401/Unauthorized
server response.
Where would I look to see what might be causing the authorization error?
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplication3IT {
private final Logger log = LoggerFactory.getLogger(DemoApplication3IT.class);
@Autowired
private TestRestTemplate restTemplate;
@Test
public void getFunky() throws IOException {
ResponseEntity<String> response =
this.restTemplate.getForEntity("http://localhost:8080/api/funky", String.class);
log.info("TestRestTemplate response Code: " + response.getStatusCode());
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); // Expected :200, Actual :401
}
}
It's strange that hitting the endpoint with a browser works fine, but the integration test fails.