Spain needed substitute Iago Aspas to score a late goal to beat Tunisia 1-0 in their final warm-up for the World Cup on Saturday.
Sergio Busquets finally undid Tunisia's defence with a long pass, and Diego Costa set up Aspas to rifle in a low strike.
Spain remained undefeated through 20 matches under coach Julen Lopetegui.
It starts the World Cup with a highly anticipated clash against neighbour Portugal next Friday.
Tunisia, which opens against England in nine days, frustrated the 2010 world champion with disciplined defending until Busquets spotted Costa's run behind the backline.
Aspas, who was Spain's sixth and final substitution in the 76th, scored the winner with six minutes remaining.
Spain will also play Iran and Morocco in Group B, while Tunisia will also face Belgium and Panama in Group G.
Alvaro Odriozola, who started for Spain at right back for the injured Dani Carvajal, misjudged a pass by Thiago Alcantara in the 13th minute to gift Tunisia its best opportunity. Ferjani Sassi, however, shot weakly into the arms of David de Gea.
The African side went close again after attacking again down Odriozola's side after robbing the ball from Busquets, only for Naim Sliti to fail to make contact with a cross with only de Gea to beat.
Lopetegui made three changes at halftime, sending on Jorge "Koke" Resurreccion and Lucas Vazquez for Thiago and Francisco "Isco" Alarcon, and Nacho Fernandez for Odriozola.
Moments after Silva volleyed a shot from a tight angle past the post, the midfielder was replaced by Marco Asensio, while Costa substituted Rodrigo Moreno with half an hour remaining.
With Spain not improving, Lopetegui sent on striker Aspas for Jordi Alba and switched to a three-man defence.