I am trying to write a Mockito Unit Test for my Spring security config class and it has this code
<code>@Bean
public GrantedAuthoritiesMapper authoritiesMapper() {
return (authorities) -> {
Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
authorities.forEach(authority -> {
if (OAuth2UserAuthority.class.isInstance(authority)) {
OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;
Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
userAttributes.getOrDefault("field", List.of())).forEach(a-> mappedAuthorities.add( new SimpleGrantedAuthority(a)));
}
});
return mappedAuthorities;
};
}
</code>
<code>@Bean
public GrantedAuthoritiesMapper authoritiesMapper() {
return (authorities) -> {
Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
authorities.forEach(authority -> {
if (OAuth2UserAuthority.class.isInstance(authority)) {
OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;
Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
userAttributes.getOrDefault("field", List.of())).forEach(a-> mappedAuthorities.add( new SimpleGrantedAuthority(a)));
}
});
return mappedAuthorities;
};
}
</code>
@Bean
public GrantedAuthoritiesMapper authoritiesMapper() {
return (authorities) -> {
Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
authorities.forEach(authority -> {
if (OAuth2UserAuthority.class.isInstance(authority)) {
OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;
Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
userAttributes.getOrDefault("field", List.of())).forEach(a-> mappedAuthorities.add( new SimpleGrantedAuthority(a)));
}
});
return mappedAuthorities;
};
}
And this is my unit test method which does not cover any line of above code.
<code>@Before(){
this.config = new Config(); //this is Config class which has authoritiesMapper
}
@Test
public void testMapper(){
//I had some mockito code here but nothing worked.
this.config.authoritiesMapper();
}
</code>
<code>@Before(){
this.config = new Config(); //this is Config class which has authoritiesMapper
}
@Test
public void testMapper(){
//I had some mockito code here but nothing worked.
this.config.authoritiesMapper();
}
</code>
@Before(){
this.config = new Config(); //this is Config class which has authoritiesMapper
}
@Test
public void testMapper(){
//I had some mockito code here but nothing worked.
this.config.authoritiesMapper();
}
If I write any Mockito test case, nothing invokes whole method, maybe because of lambda expression. How to write a Mockito test when lambda expression like this exist??