How does this even work? As per my understanding it shouldn't have. LDAPGroupAccessor is being new initialized in class or can be new initialized in constructor itself, it is not being injected, is not a constructor argument, not a spring bean injection.
I know it is possible using reflection, but how is injectMocks injecting it ? Doesn't this defeat the purpose of DI ?
@Component
public class AuthorizationHandler {
private LDAPGroupAccessor groupAccessor = new LDAPGroupAccessor();
public isUserAuthorized(String userId, String groupId){
return groupAccessor.isUserInGroup(userId, ldapGroup);
}
}
public class AuthorizationHandlerTest {
@InjectMocks
private AuthorizationHandler authorizationHandler;
@Mock
private LDAPGroupAccessor groupAccessor = new LDAPGroupAccessor();
@Before
public void setup() {
String authorizedUser = "authorizedUser";
Mockito.when(groupAccessor.isUserInGroup("authorizedUser", "someGroup")).thenReturn(true);
}
@Test
public void test1() {
Assert.assertEquals(true, authorizationHandler.isUserAuthorized("authorizedUser", "someGroup"));
}
}