### Question:
UserProfileManagerAspect { @AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.getUser(..))", returning = "user") public void injectProfile(Object user) { if (user != null) { AbstractUser userDetails = (AbstractUser) user; if (null == userDetails.getProfile()) { try { IUserProfile profile = this.getUserProfileManager().getProfile(userDetails.getUsername()); userDetails.setProfile(profile); } catch (Throwable t) { logger.error("Error injecting profile on user {}", userDetails.getUsername(), t); } } } }  @AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.getUser(..))", returning = "user") void injectProfile(Object user); @AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.addUser(..)) && args(user,..)") void addProfile(Object user); @AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.updateUser(..)) && args(user,..)") void updateProfile(Object user); @AfterReturning(pointcut = "execution(* com.agiletec.aps.system.services.user.IUserManager.removeUser(..)) && args(key)") void deleteProfile(Object key); void setUserProfileManager(IUserProfileManager userProfileManager);  }### Answer:
@Test public void testInjectProfile_1() throws ApsSystemException { IUserProfile returned = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE); when(userProfileManager.getProfile(Mockito.anyString())).thenReturn(returned); User user = new User(); user.setUsername("test"); Assert.assertNull(user.getProfile()); userProfileManagerAspect.injectProfile(user); Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test"); IUserProfile profile = (IUserProfile) user.getProfile(); Assert.assertNotNull(profile); Assert.assertEquals("test", profile.getUsername()); }
@Test public void testInjectProfile_2() throws ApsSystemException { when(userProfileManager.getProfile(Mockito.anyString())).thenThrow(ApsSystemException.class); User user = new User(); user.setUsername("test"); Assert.assertNull(user.getProfile()); userProfileManagerAspect.injectProfile(user); Mockito.verify(userProfileManager, Mockito.times(1)).getProfile("test"); Assert.assertNull(user.getProfile()); }
@Test public void testInjectProfile_3() throws ApsSystemException { when(userProfileManager.getProfile(Mockito.anyString())).thenThrow(ApsSystemException.class); IUserProfile profile = this.createFakeProfile("test", SystemConstants.DEFAULT_PROFILE_TYPE_CODE); User user = new User(); user.setUsername("test"); user.setProfile(profile); userProfileManagerAspect.injectProfile(user); Mockito.verify(userProfileManager, Mockito.times(0)).getProfile("test"); Assert.assertNotNull(user.getProfile()); Assert.assertSame(profile, user.getProfile()); }