叶わぬ時の神頼み
Meanings
1
turning to God only in one's trouble; praying only when in need
Proverbial expression criticizing those who seek divine help only when facing difficulties, ignoring faith in good times. Often used to describe opportunistic or last-resort reliance on religion.
Going to a shrine only before exams, when you're not usually devout, is a perfect example of turning to God only in your trouble.
He only prays when he's in trouble—a classic case of turning to God only in one's trouble.