### Question:
OAuth2TokenDAO extends AbstractSearcherDAO implements IOAuth2TokenDAO { @Override public OAuth2RefreshToken readRefreshToken(String tokenValue) { FieldSearchFilter filter = new FieldSearchFilter("refreshtoken", tokenValue, true); FieldSearchFilter[] filters = {filter}; List<String> accessTokens = super.searchId(filters); if (null != accessTokens && accessTokens.size() > 0) { return new DefaultOAuth2RefreshToken(tokenValue); } return null; }  @Override List<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String username); @Override List<OAuth2AccessToken> findTokensByUserName(String username); @Override List<OAuth2AccessToken> findTokensByClientId(String clientId); @Override void storeAccessToken(OAuth2AccessToken accessToken, OAuth2Authentication authentication); @Override OAuth2AccessToken readAccessToken(final String accessToken); @Override void removeAccessToken(final String accessToken); @Override void removeAccessTokenUsingRefreshToken(String refreshToken); @Override void deleteExpiredToken(int expirationTime); @Override OAuth2RefreshToken readRefreshToken(String tokenValue); @Override OAuth2Authentication readAuthenticationForRefreshToken(OAuth2RefreshToken refreshToken);  }### Answer:
@Test public void readRefreshToken() throws Exception { when(this.statForSearchId.executeQuery()).thenReturn(resForSearchId); Mockito.when(resForSearchId.next()).thenReturn(true).thenReturn(false); Mockito.when(resForSearchId.getString(Mockito.anyString())).thenReturn("refresh_token"); OAuth2RefreshToken refreshToken = this.tokenDAO.readRefreshToken("refresh_token"); Assert.assertNotNull(refreshToken); Assert.assertEquals("refresh_token", refreshToken.getValue()); Mockito.verify(statForSearchId, Mockito.times(1)).setString(Mockito.anyInt(), Mockito.anyString()); Mockito.verify(resForSearchId, Mockito.times(1)).getString(Mockito.anyString()); this.executeFinalCheckForSearchId(true); Mockito.verify(conn, Mockito.times(1)).close(); }
@Test public void readRefreshToken_noteExists() throws Exception { when(this.statForSearchId.executeQuery()).thenReturn(resForSearchId); Mockito.when(resForSearchId.next()).thenReturn(false); OAuth2RefreshToken refreshToken = this.tokenDAO.readRefreshToken("refresh_token"); Assert.assertNull(refreshToken); Mockito.verify(statForSearchId, Mockito.times(1)).setString(Mockito.anyInt(), Mockito.anyString()); Mockito.verify(resForSearchId, Mockito.times(0)).getString(Mockito.anyString()); this.executeFinalCheckForSearchId(true); Mockito.verify(conn, Mockito.times(1)).close(); }
@Test(expected = RuntimeException.class) public void failReadRefreshToken() throws Exception { try { when(this.statForSearchId.executeQuery()).thenReturn(resForSearchId); when(resForSearchId.next()).thenReturn(true).thenReturn(false); when(resForSearchId.getString(Mockito.anyString())).thenThrow(SQLException.class); OAuth2RefreshToken refreshToken = this.tokenDAO.readRefreshToken("refresh"); Assert.fail(); } catch (RuntimeException e) { Mockito.verify(statForSearchId, Mockito.times(1)).setString(Mockito.anyInt(), Mockito.anyString()); Mockito.verify(resForSearchId, Mockito.times(1)).getString(Mockito.anyString()); this.executeFinalCheckForSearchId(true); Mockito.verify(conn, Mockito.times(1)).close(); throw e; } }