'Abdullah bin 'Umar bin Al-Khattab (May Allah be pleased with them) narrated that:
He heard Messenger of Allah (ﷺ) as saying: "Three men, amongst those who came before you, set out until night came and they reached a cave, so they entered it. A rock fell down from the mountain and blocked the entrance of the cave. They said: 'Nothing will save you from this unless you supplicate to Allah by virtue of a righteous deed you have done.' Thereupon, one of them said: 'O Allah! I had parents who were old, and I used to offer them milk before any of my children or slaves. One day, I went far away in search of grazing and could not come back until they had slept. When I milked as usual and brought the drink I found them both asleep. I hated to disturb them and also disliked to give milk to my children before them. My children were crying out of hunger at my feet but I awaited with the bowl in my hand for them to wake up. When they awoke at dawn, they drank milk. O Allah! If I did so to seek Your Pleasure, then deliver us from the distress caused by the rock'. The rock moved slightly but they were unable to escape. The next said: 'O Allah! I had a cousin whom I loved more than any one else (in another version he said: as a man can love a woman). I wanted to have sexual intercourse with her but she refused. Hard pressed in a year of famine, she approached me. I gave her one hundred and twenty dinars on condition that she would yield herself to me. She agreed and when we got together (for sexual intercourse), she said: Fear Allah and do not break the seal unlawfully. I moved away from her in spite of the fact that I loved her most passionately; and I let her keep the money I had given her. O Allah! If I did that to seek Your Pleasure, then, remove the distress in which we are.' The rock moved aside a bit further but they were still unable to get out. The third one said: 'O Allah! I hired some labourers and paid them their wages except one of them departed without taking his due. I invested his money in business and the business prospered greatly. After a long time, he came to me and said: O slave of Allah! Pay me my dues. I said: All that you see is yours - camels, cattle, goats and slaves. He said: O slave of Allah! Do not mock at me. I assured him that I was not joking. So he took all the things and went away. He spared nothing. O Allah! If I did so seeking Your Pleasure, then relieve us of our distress.' The rock slipped aside and they got out walking freely".[Al-Bukhari and Muslim].
Arabic/English book reference: Book 1, Hadith 12