Imams Bukhari and Muslim reported that the Messenger sallallahu `alayhi wa sallam said:
“Three persons of a people before you, were on a journey when they were overtaken by a storm and therefore they took shelter in a cave. A rock slipped down from the mountain and blocked the exit from cave.
One of them said: “The only way for deliverance left is to beseech Allah in the name of some virtuous deed.” Thereupon one of them supplicated, “O Lord, my parents were very old, I used to offer them their nightly drink of milk before my children and the other members of the family. One day I went astray far away in search of green trees and could return only after my parent had gone to sleep. When I had milked the animals and brought their nightly drink to them, They were fast asleep, but I did not like to disturb them, nor would give any part of the milk to my children and other members of the family till after my parents had their drink. Thus, with the vessel in hand, I awaited their awakening till the flush of dawn, While the children cried out of hunger at my feet. When they woke up, they had their drink. O Lord, if I did this thing seeking only your pleasure, then do relieve us of the distress wrought upon us by this rock.” Thereupon, The rock moved a little but that not enough to let them pass out.
Then the second man supplicated: “O Lord, I had a cousin whom I loved her more passionately than any loves a woman. I tried to seduce her but she would have none of me, till in a season of great hardship due to famine, she approached me (for help) and I gave her one hundred and twenty Dinars on condition that she would have sexual intercourse with me. She agreed, and when we got together and I was just going to have intercourse with her, she pleaded: ‘Fear Allah, and do not break the seal unlawfully’; whereupon I moved away from her, despite the fact that I desired her most passionately; and I let her keep the money I had given her. O Lord, if I did this thing seeking only your pleasure, then do move the distress in which we find ourselves.” Again the rock moved a little but not enough to let them pass out.
Then the third supplicated: “O Lord, I hired some laborers and paid them their dues, but one of them left leaving behind what was due to him. I invested it in business and the business prospered greatly.” After a time the laborer came Back and said: ‘O servant of Allah, hand over to me my wages.’ I said to him: ‘All that you see is yours – camels, cattle, goats and slaves.’ He said: ‘Don’t play joke with me, O servant of Allah.’ I assured him: ‘I am mot joking.’ So he took all of it sparing nothing. O Lord, If I did this seeking only for your pleasure, do relieve us of our distress.”
The rock then moved away, and all the three came out of the cave safe and sound.
(Sahih Bukhari and Sahih Muslim )