ChainedUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername( String s ) throws UsernameNotFoundException, DataAccessException { for ( UserDetailsService delegate : delegates ) { try { UserDetails details = delegate.loadUserByUsername( s ); if ( details != null ) { return details; } } catch ( UsernameNotFoundException ignored ) { } } throw new UsernameNotFoundException( Messages.getInstance().getString( "UserRoleDaoUserDetailsService.ERROR_0001_USER_NOT_FOUND" ) ); } ChainedUserDetailsService( List<UserDetailsService> delegates ); @Override UserDetails loadUserByUsername( String s );  }
@Test public void testLoadUserByUsername() throws Exception { final UserDetailsService mock1 = mock( UserDetailsService.class ); UserDetails joeDetails = mock( UserDetails.class ); UserDetails adminDetails = mock( UserDetails.class ); when( mock1.loadUserByUsername( "joe" ) ).thenReturn( joeDetails ); final UserDetailsService mock2 = mock( UserDetailsService.class ); when( mock1.loadUserByUsername( "admin" ) ).thenReturn( adminDetails ); ChainedUserDetailsService userDetailsService = new ChainedUserDetailsService( Arrays.asList( mock1, mock2 ) ); final UserDetails joe = userDetailsService.loadUserByUsername( "joe" ); assertSame( joeDetails, joe ); final UserDetails admin = userDetailsService.loadUserByUsername( "admin" ); assertSame( adminDetails, admin ); }