Reputation: 629
I am trying to write a test for my controller but the test environment fails to load with the given stackTrace.
@PreAuthorize("hasAuthority('my.scope')")
@GetMapping(value = "/path", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Set<String>>> getPath() {
return myService.pathFunction()
.map(ResponseEntity::ok);
}
and following is how I've configured my security config
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebConfig extends WebSecurityConfigurerAdapter {
private static final String PRINCIPAL = "sub";
@Override
protected void configure(HttpSecurity http) throws Exception {
JwtAuthenticationConverter authenticationConverter =
new JwtAuthenticationConverter();
authenticationConverter.setJwtGrantedAuthoritiesConverter(...);
authenticationConverter.setPrincipalClaimName(PRINCIPAL);
http.csrf().disable()
.cors()
.and()
.authorizeRequests().antMatchers("/actuator/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(authenticationConverter);
}
}
The controller is working as needed but my test fails with this error. I am not using any customer JwtDecoder
@WebMvcTest(controllers = Controller.class)
class ControllerTest {
@MockBean
private MyService myService;
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser(authorities = {"my.scope"})
void controllerTest() throws Exception{
Map<String, Set<String>> mapResponse = Map.of("key", Set.of("foo1", "foo2"));
Mockito.when(myService.pathFunction()).thenReturn(Optional.of(mapResponse));
MockHttpServletResponse result = mockMvc.perform(get("/configuration/api-registry")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn()
.getResponse();
String response = result.getContentAsString();
assertTrue(response.contains("foo1"));
}
}
How do I run this test ?
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 90 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available
at ...
Upvotes: 19
Views: 20609
Reputation: 111
This configuration test also resolved problem in Unit Tests.
oauth2:
resourceserver:
jwt:
issuer-uri: test
Upvotes: 11
Reputation: 186
Hi i was having same issue, then I realize on my test properties I missed to configure:
oauth2:
resourceserver:
jwt:
issuer-uri: realm_uri_here
jwk-set-uri: ${spring.security.oauth2.resource-server.jwt.issuer-uri}/protocol/openid-connect/certs```
Upvotes: 10
Reputation: 791
I got this error when using @SpringBootTest
. It started appearing when upgrading from spring boot version 2.5.12
to 2.7.8
.
I could fix it by changing
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
to
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Upvotes: 1
Reputation: 629
I added mocking the decoder in the test as
@MockBean
private JwtDecoder jwtDecoder;
and the test ran !
Upvotes: 28