Hello everyone in my application im using oauth2.0 for authentication in my controller methods im always checking if Authentication argument is always set.
public ResponseEntity<?> getFarm(Authentication authentication) {
if (authentication == null) {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
everything works fine but im trying to write integration test for this and I have problem with mocking the authentication i was trying many solutions there like : Spring Test & Security: How to mock authentication? but still im getting null.
this is my integration test
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestRest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void findAllCustomers() {
var farmInfoDto = new FarmInfoDto("big farm", "warsaw", "strett", "123", "15a");
var test = restTemplate.withBasicAuth("test","test").postForEntity("/farm", farmInfoDto, Farm.class);
var farm = test.getBody();
assert farm.getFarmName().equals(farmInfoDto.farmName());
}
}