It is well known in the Church that God tests us through trials. Sometimes that test is made more difficult when we do not know the reason for the trial.
God is building character in us, and part of that character is the trait of believing and trusting God even when circumstances and our own reasoning tempt us into thinking that God is wrong. In order to test our faith and develop the character that will trust God implicitly, God sometimes puts us through trials that make it APPEAR. that He is not keeping His word to us, that He is unfaithful to us. God lets us go through circumstances that might appear to our carnal and limited human reasoning that God is unjust. But God is never unjust. God is perfect in righteousness and judgment, and His decisions are always right. We have to choose to trust and believe in God and His righteousness and perfect judgment in every kind of trial, even when we do not understand the purpose or reason for the trial.
There are lessons in the book of Job about suffering through trials. Job is perhaps the greatest example in the Bible of man who went through a trial from God that he did not understand.
Job is often spoken of as an example of a man who had self-righteousness. I have not found the term "self-righteous" or "self-righteousness" in the Bible in the King James or New King James versions. One speaker I heard said that Job was self-righteous, then said that self-righteous people tend to be critical of others. I have noticed that this is how the term self-righteous is often used, to denote someone who feels they are superior to others and tends to criticize and find fault with others. If this is what self-righteousness is, it would apply as much or more to Job's three friends as to Job. They were finding fault with Job where there was none (Job 32:3).
But Job had trouble trusting in the righteousness of God. Job was righteous, according to God's own testimony, and God had blessed Job (Job 1:8-10, 1-3, 31:1-40). Then when God took away Job's blessings, Job did not understand why God had done so. He knew this was from God, but he could not understand why. As the trial went on, Job was tempted to charge God with injustice (Job 27:2, 32:2, 34:5). He thought God was less fair and less righteous than He was (Job 35:2).
But God is not unjust. He is wiser than us and we have to trust His wisdom, justice, and righteousness more than our own, and that was a lesson Job had to learn.
It was easy for Job to believe that God was good when God was blessing him for his obedience. But when God allowed him to suffer greatly for a long time in an unusual way even though Job had not done anything wrong, Job's faith in God's goodness was tested. It seemed to him that God was treating him unfairly. He could not think of anything he could have done wrong, and he questioned God's fairness. When faced with the choice of believing in his own righteousness or God's righteousness, he believed in his own righteousness more than God's. But eventually he learned his lesson and God blessed him again.
The lesson Job had to learn was the lesson of faith.
God often tests the faith of those who believe in Him. The biggest part of faith is not just believing that God exists, but believing God, in other words, trusting God enough to believe what He says even when we do not yet understand the "why", and believing in God's fairness and righteousness. This is a lesson God wants us to learn in this life because it will pay off for all eternity.