The win restored the optimism of the world champions and took away the bad taste of last week's surprise 3-1 defeat at home to Slovakia.
"The victory gives us stability and a good feeling," Germany coach Joachim Loew said.
At the Euros, Germany will be in Group C, along with Ukraine, Poland and Northern Ireland. They will make their debut June 12 against Ukraine in Lille.
Hungary, who had not reached a major football tournament since 1986, will be in Group F with Austria, Portugal and Iceland, and will start June 14 against Austria in Bordeaux.
Later on Saturday, Croatia flexed their muscles with a 10-0 slaughter of feeble San Marino, with a hat-trick by Mario Mandzukic followed by a brace from Nikola Kalinic.
Croatia will be in Group D along with holders Spain, Czech Republic and Turkey.
Also Saturday, Austria - who have been absent from football's big stages since 1998 - crashed 2-0 at home to fallen giants Netherlands, who are the big absentee from the finals, while Slovakia were held 0-0 by defiant Northern Ireland.