"Now Moses was keeping the flock" (Exodus 3:1). It is written, "The LORD tries the righteous" (Psalms 11:5). Rabbi Isaac said: With what does He try the righteous? With the tending of flocks. David was tried with shepherding, as it is said, "From following the ewes that give suck He brought him, to be shepherd over Jacob His people." Amos was tried with shepherding, as it is said, "And the LORD took me from following the flock" (Amos 7:15). Moses too was tried with shepherding, as it is said, "Now Moses was keeping the flock." "And he led the flock to the far side of the wilderness" (Exodus 3:1) — to keep them far from robbery [grazing on others' land].
How God Tests the Righteous Through Shepherding
Yalkut Shimoni on Torah 169:7
(שמות ג א-ה) וּמֹשֶׁה הָיָה רֹעֶה, כְּתִיב (תהלים יא, ה) "ה' צַדִּיק יִבְחָן", אָמַר רַבִּי יִצְחָק, בַּמֶּה בּוֹחֵן אֶת הַצַּדִּיקִים, בְּמִרְעֶה. דָּוִד נִבְחָן בְּמִרְעֶה, שֶׁנֶּאֱמַר "מֵאַחַר עָלוֹת הֵבִיאוֹ לִרְעוֹת בְּיַעֲקֹב עַמּוֹ". [עָמוֹס] נִבְחָן בְּמִרְעֶה, שֶׁנֶּאֱמַר (עמוס ז, טו) "וַיִּקָּחֵנִי ה' מֵאַחֲרֵי הַצֹּאן". אַף מֹשֶׁה נִבְחָן בְּמִרְעֶה, שֶׁנֶּאֱמַר וּמשֶׁה הָיָה רֹעֶה. וַיִּנְהַג אֶת הַצֹּאן אַחַר הַמִּדְבָּר, לְהַרְחִיק מִן הַגָּזֵל.