code: @Test public void setAuthenticatedSessionAttribute(){
    String name=CachedAuthenticatedSessionHandler.class.getName() + ".AuthenticatedSession";
    Account account=mock(Account.class);
    AuthenticatedSession auth=new AuthenticatedSession(account,HttpServletRequest.FORM_AUTH);
    this.validate(session -> session.setAttribute(name,"bar"));
    SessionManager<LocalSessionContext,Batch> manager=mock(SessionManager.class);
    Batcher<Batch> batcher=mock(Batcher.class);
    BatchContext context=mock(BatchContext.class);
    SessionAttributes attributes=mock(SessionAttributes.class);
    Account oldAccount=mock(Account.class);
    AuthenticatedSession oldAuth=new AuthenticatedSession(oldAccount,HttpServletRequest.FORM_AUTH);
    ArgumentCaptor<AuthenticatedSession> capturedAuth=ArgumentCaptor.forClass(AuthenticatedSession.class);
    when(this.manager.getSessionManager()).thenReturn(manager);
    when(manager.getBatcher()).thenReturn(batcher);
    when(batcher.resumeBatch(this.batch)).thenReturn(context);
    when(this.session.getAttributes()).thenReturn(attributes);
    when(attributes.setAttribute(same(name),capturedAuth.capture())).thenReturn(oldAuth);
    AuthenticatedSession result=(AuthenticatedSession)this.adapter.setAttribute(name,auth);
    assertSame(auth.getAccount(),capturedAuth.getValue().getAccount());
    assertSame(auth.getMechanism(),capturedAuth.getValue().getMechanism());
    assertSame(oldAccount,result.getAccount());
    assertSame(HttpServletRequest.FORM_AUTH,result.getMechanism());
    verify(context).close();
    reset(context,attributes);
    capturedAuth=ArgumentCaptor.forClass(AuthenticatedSession.class);
    when(attributes.setAttribute(same(name),capturedAuth.capture())).thenReturn(null);
    result=(AuthenticatedSession)this.adapter.setAttribute(name,auth);
    assertSame(auth.getAccount(),capturedAuth.getValue().getAccount());
    assertSame(auth.getMechanism(),capturedAuth.getValue().getMechanism());
    assertNull(result);
    verify(context).close();
    reset(context,attributes);
    auth=new AuthenticatedSession(account,HttpServletRequest.BASIC_AUTH);
    AuthenticatedSession oldSession=new AuthenticatedSession(oldAccount,HttpServletRequest.BASIC_AUTH);
    LocalSessionContext localContext=mock(LocalSessionContext.class);
    when(this.session.getLocalContext()).thenReturn(localContext);
    when(localContext.getAuthenticatedSession()).thenReturn(oldSession);
    result=(AuthenticatedSession)this.adapter.setAttribute(name,auth);
    verify(localContext).setAuthenticatedSession(same(auth));
    verify(context).close();
  }
  