Allah says in the Quran:
 
أَمۡ حَسِبۡتُمۡ أَن تَدۡخُلُواْ ٱلۡجَنَّةَ وَلَمَّا يَأۡتِكُم مَّثَلُ ٱلَّذِينَ خَلَوۡاْ مِن قَبۡلِكُم‌ۖ مَّسَّتۡہُمُ ٱلۡبَأۡسَآءُ وَٱلضَّرَّآءُ وَزُلۡزِلُواْ حَتَّىٰ يَقُولَ ٱلرَّسُولُ وَٱلَّذِينَ ءَامَنُواْ مَعَهُ ۥ مَتَىٰ نَصۡرُ ٱللَّهِ‌ۗ أَلَآ إِنَّ نَصۡرَ ٱللَّهِ قَرِيبٌ۬
 
“Or do you think that you shall enter the Garden (of Bliss) without such (trials) as came to those who passed away before you? They encountered suffering and adversity and were so shaken in spirit that even the Messenger and those of faith who were with him cried: “When (will come) the help of Allah?” Ah! verily the help of Allah is (always) near!” (2:214)
 
وَلَنَبۡلُوَنَّكُم بِشَىۡءٍ۬ مِّنَ ٱلۡخَوۡفِ وَٱلۡجُوعِ وَنَقۡصٍ۬ مِّنَ ٱلۡأَمۡوَٲلِ وَٱلۡأَنفُسِ وَٱلثَّمَرَٲتِ‌ۗ وَبَشِّرِ ٱلصَّـٰبِرِينَ
 
“We shall test you with something of fear and hunger, some loss in goods, lives and the fruits (of your toil), but give glad tidings to those who patiently persevere.” (2:155)
 
Allah further says:
 
أَمۡ حَسِبۡتُمۡ أَن تَدۡخُلُواْ ٱلۡجَنَّةَ وَلَمَّا يَعۡلَمِ ٱللَّهُ ٱلَّذِينَ جَـٰهَدُواْ مِنكُمۡ وَيَعۡلَمَ ٱلصَّـٰبِرِينَ
 
“Did you think that ye would enter Heaven without Allah testing those of you who fought hard (in His cause) and remained steadfast?” (3: 142)
 
And:
 
أَحَسِبَ ٱلنَّاسُ أَن يُتۡرَكُوٓاْ أَن يَقُولُوٓاْ ءَامَنَّا وَهُمۡ لَا يُفۡتَنُونَ  وَلَقَدۡ فَتَنَّا ٱلَّذِينَ مِن قَبۡلِهِمۡ‌ۖ فَلَيَعۡلَمَنَّ ٱللَّهُ ٱلَّذِينَ صَدَقُواْ وَلَيَعۡلَمَنَّ ٱلۡكَـٰذِبِينَ
 
“Do people think that they will be left alone on saying “We believe” and that they will not be tested? We did test those before them, and Allah will certainly know those who are true from those who are false.” (29:2-3)
 
See also: 2:497:1418:1721:3547:31
 
Some people express surprise when they are confronted with hardships or various types of distress or disappointments (Bala’, Musibaat, Fitan and Iqab) and they begin to question Allah. They may ask: “Why is Allah doing this to me?” Or, “Why is Allah allowing this to happen?”
 
There are some people who feel that Allah owes them something for their good works or for living an upright life so in their minds they should be repaid with goodness, such as material wealth or good health, and be spared from hardships. But it does not always work out that way. Look at the righteous people of the past, including the prophets of Allah, and reflect upon what they went through. Study the seerah of the Prophet (pbuh) and read about the kinds of hardships and heartaches that he (pbuh) encountered. Sadly, few of us take the time to study the seerah in any depth and possess only a superficial understanding of the Prophet’s (pbuh) life and mission.
 
The reality of the matter is that living a righteous life is not a protection against hardships and trials. In fact, as the above verses indicate, Allah promised that we would be tested and experience suffering and loss. And Allah’s promise is true, so we should not express surprise when what He has promised has come to pass or automatically interpret these hardships and trials as manifestations of His displeasure towards us.

And recall that on the authority of Suhaib (may Allah be pleased with him) he said: The Prophet (pbuh) said:
 
“Amazing is the affair of the believer, verily all of his affair is good and this is not for no one except the believer. If something of good/happiness befalls him he is grateful and that is good for him. If something of harm befalls him he is patient and that is good for him” (Muslim)
 
We need to think well of our Creator and ask Him for patience and strength and know that la hawla wala quwwata illa billah — there is no might nor power except with Allah. 
 
And know that Allah says in a Hadeeth Qudsi:  “Allah the Almighty said: I am as My servant thinks I am.” (al-Buhkari — also by Muslim, at-Tirmidhi and Ibn-Majah)
 
Allah only wants what is good for us, and only He knows what that is as His knowledge encompasses all things while our knowledge is very limited and superficial. Allah tells us: “Perhaps you dislike a thing and Allah makes therein much good.” [Qur’an 4: 19]. 
 
And also know that the Prophet (pbuh) said: “No calamity befalls a Muslim but that Allah expiates some of his sins because of it, even if it were the prick of a thorn.”  [Bukhari, Muslim].
 
And the prophet (pbuh) further said: “The people who face the most difficult tests are the prophets, then the righteous, then those following them in degree. A person is tried according to his religion. So if there is firmness in his religion, then the trial is increased, and if there is a weakness, then it is lightened. Verily a trial remains with a servant until he walks the earth having no sin left upon him.” [Bukhari].
 
May Allah make us among those who are steadfast and call upon Him at all times.