@Test public void loadUserExist() { // 准备数据 User user = new User(); user.setLoginName("admin"); user.setShaPassword(new ShaPasswordEncoder().encodePassword("admin", null)); Role role1 = new Role(); role1.setName("admin"); Role role2 = new Role(); role2.setName("user"); user.getRoleList().add(role1); user.getRoleList().add(role2); // 录制脚本 EasyMock.expect(mockAccountManager.findUserByLoginName("admin")).andReturn(user); control.replay(); // 执行测试 OperatorDetails operator = (OperatorDetails) userDetailsService.loadUserByUsername(user.getLoginName()); // 校验结果 assertEquals(user.getLoginName(), operator.getUsername()); assertEquals(new ShaPasswordEncoder().encodePassword("admin", null), operator.getPassword()); assertEquals(2, operator.getAuthorities().size()); assertEquals( new GrantedAuthorityImpl("ROLE_admin"), operator.getAuthorities().iterator().next()); assertNotNull(operator.getLoginTime()); }
@Test(expected = UsernameNotFoundException.class) public void loadUserByWrongUserName() { // 录制脚本 EasyMock.expect(mockAccountManager.findUserByLoginName("foo")).andReturn(null); control.replay(); assertNull(userDetailsService.loadUserByUsername("foo")); }
@Before public void setUp() { userDetailsService = new UserDetailsServiceImpl(); mockAccountManager = control.createMock(AccountManager.class); userDetailsService.setAccountManager(mockAccountManager); }