I have followed the accepted workaround on Spring Boot Authentication for Integration Tests and apply the following approach to authenticate requests for Integration Tests:
@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@ActiveProfiles("test")
@EnableAutoConfiguration(
exclude = {
SecurityAutoConfiguration.class,
SecurityFilterAutoConfiguration.class,
// FallbackWebSecurityAutoConfiguration.class, // cannot find this ref
OAuth2ClientAutoConfiguration.class
})
abstract public class IntegrationTest {
@Autowired
protected MockMvc mvc;
@MockBean
protected AuthService authService;
@MockBean
protected JwtUtils jwtUtils;
And test class:
class ProductControllerTest extends IntegrationTest {
@Test
void findById() throws Exception {
mvc.perform((get("/api/v1/products/{id}", 1)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.name", equalTo("Laptop")));
}
However, it does not make any sense and still gives "AuthEntryPointJwt : Unauthorized error: Full authentication is required to access this resource" error.
I also tried to use second option via application-test.yml
:
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
- org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration
- org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration
- org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration