He did this only to teach warfare to the descendants of the Israelites who had not had previous battle experience. Judges 3:2
God brought the nation of Israel into the Promised Land of Canaan through Joshua. After Joshua, there came an entire generation who had very little experience in fighting battles like the ones fought by Joshua. Training and testing God's people is one of God's important strategies that enable His children to succeed in spiritual warfare. This is why we do not live a life free of trials. These trials are sent specifically to see if our faith is real or simply empty words.
"They were left to test the Israelites to see whether they would obey the Lord's commands, which He had given their forefathers through Moses" (Judg. 3:4). God allows circumstances to develop around your life to give your faith opportunity to be proven. It is only when we are tested in battle that we become skilled warriors. You can be confident God will allow trials to come your way through situations like an unreasonable boss, a vendor who refuses to pay, a false assault on your character, or a difficult relationship that requires unconditional love. These battles are sent your way to test what you know in the mind in order that they might become part of your heart.
You will discover if you have passed the tests or if you need more battles that will give you the opportunity to learn the art of spiritual warfare. Do not fear these battles that are before you. God has already given you the victory if you choose complete dependence and obedience to Him. Then you will become one of God's greatest warriors, skilled in spiritual warfare.