I have an integration test for Spring (with Spring Security) that looks like this:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RootControllerIT {
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate template;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
@Test
@WithMockUser
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(),
String.class);
assertThat(response.getBody(), equalTo("OK"));
}
}
My issue is that the @WithMockUser
does not work here, is there a way to add a mock user to the TestRestTemplte
or otherwise send authenticated user information to the Integration test?
The mock user works fine when using MockMvc
to test, but it's obviously not appropriate here.