A migrant holds up a picture of German Chancellor Angela Merkel after arriving at the main railway station in Munich, in 2015. (Michael Dalder/Reuters)

BERLIN — Barring something seismic and unpredictable, Angela Merkel, Germany’s long-serving chancellor, will be reelected Sunday.

That hasn't always been clear. Scarcely a year ago, pundits were preparing her political obituary.

In September 2016, the chancellor's Christian Democratic Union fell to third place in an election in her home district. Overtaking her center-right bloc was Alternative for Germany (AfD), the nationalist, anti-immigrant party that has pilloried Merkel for her decision in 2015 not to close the country's borders to about a million refugees seeking entry into Germany.

The vote took place a year after Merkel, together with Austrian leaders, agreed to admit asylum seekers trapped in Hungary. A referendum of sorts on that decision, the state election delivered a harsh verdict: It was the first time in modern Germany that Merkel’s Christian Democratic Union, and its Bavarian counterpart, the Christian Social Union, had been outdone by a far-right party.

The vote unfolded between the decision of British voters to leave the European Union and the election of Donald Trump. Signaling a rejection of globalization and an embrace of nativism, both events ran counter to Merkel's worldview.

Then, 12 people were killed in a December attack on a Christmas market in Berlin, by a failed asylum seeker known to federal prosecutors. After several international setbacks, this was a blow to the heart of Germany, unfolding just across the Tiergarten from the Chancellery, where Merkel had presided for 11 years.

Last year, Frauke Petry, AfD’s chairwoman, declared Merkel “finished.” A German journalist writing in the Financial Times called the attack a “disaster for the government but ... even more so for Ms. Merkel personally.” A former aide to Margaret Thatcher, the conservative British prime minister, predicted that Merkel would be the “biggest loser of 2017.”

Not so much. Her party enjoys a double-digit lead across surveys leading up to Sunday's decisive vote.

Many remain critical of Merkel’s response to the refugee crisis. She has been heckled on the campaign trail over this issue.

Just like the vote last fall, the federal election this month is a referendum of sorts on the chancellor’s handling of the refugee crisis. Merkel herself avows that the events of 2015 cannot be repeated. But she seems unlikely to suffer at the polls as a result of them.

Here are five reasons why.

1. The numbers are under control

After about a million migrants entered Germany in 2015, only 280,000 claimed asylum in 2016. Just 90,389 followed suit in the first half of 2017. And deportations have increased.

Migrants haven’t stopped coming of their own accord. Rather, the lower figures are a result of enhanced border security in the Balkans, international dealmaking and stricter domestic rules in Germany.

In March 2016, Macedonia, Croatia and Slovenia — three of the thruways between Greece and coveted asylum destinations in northern Europe — announced that their borders had been closed. Balkan countries that had sheltered migrants on a favored path to countries like Germany were now locking migrants out. Only illegal passage continued.

That same month, the E.U.-Turkey deal took effect. Under the agreement, Ankara agreed to take back migrants from Greece, while some Syrians already in Turkey would be dispersed throughout the E.U. The agreement slowed the torrent of migrants in the summer of 2016 but has since faltered, particularly with the Turkish government’s crackdown after the July 2016 coup attempt.

Still, the deal is something of a model for new negotiations between European officials and African governments — the aim being to stem the tide of migrants crossing into Europe from North Africa. In one example, Europe is pledging $1.9 billion toward development and other forms of assistance in Africa in exchange for enhanced border security and willingness to take back deportees.

“A lot of these ideas are based on the premise that the E.U.-Turkey deal is a success, which is something we would dispute,” said Catherine Woollard, secretary general of the European Council on Refugees and Exiles, a network of advocacy groups.

Germany has also tightened rules internally, including by suspending the right of family reunification for certain categories of asylum seekers. Some of these changes have spurred legal challenges that are jamming the courts.

2. Merkel has stuck to careful messaging

Merkel is not a skilled orator. But she is a strategic communicator.

One of her stock lines on the campaign trail is to thank citizens for their help in responding to the difficult months of 2015 and 2016. It is a gesture to the German idea of Willkommenskultur, or “welcome culture,” represented most vividly in Germans who showed up at train stations and asylum centers in the fall of 2015 with water bottles, blankets and toys for new arrivals.

By thanking them, Merkel acknowledges that the refugee influx created a strain but admits no wrongdoing. Indeed, she has said in interviews that she would make the same decision again, given the circumstances, while also pledging that such conditions cannot be allowed to repeat themselves.

3. A prosperous economy blunts the impact

Immigration is an important issue to German voters — the most important issue, some polling suggests. But it’s not their sole concern.

Surveys by Infratest dimap, a polling agency in Berlin, indicate that social injustice and poverty, pensions, education and unemployment are also foundational for voters. In fact, they rank as more important than domestic security and crime.

On many of these measures, Germany is doing well. Unemployment stands at 3.7 percent. Real wages are rising. Germany reported a budget surplus in August of 18.3 billion euros, or about $21.6 billion.

The gap between the rich and the poor is widening, but inequality is not as severe as it is in the United States and Britain. Pew data released this month suggests that overwhelming majorities hold favorable opinions of the country's economic performance — and perhaps by extension, most people approve of its mainstream parties.

A case in point is a septuagenarian, Uli Heinemann, in Anklam, a stronghold of the far right in the east, who said he once backed the AfD out of concern that refugees would overrun Germany at a time when “many young men don't have jobs.” But that hasn't proved to be true, he said, so he plans to back Merkel's party this time around.

4. The chancellor's opponents have faltered

Merkel’s main rivals are not popular enough to threaten her leadership.

After the AfD scored a string of strong showings in local and state elections, it faltered as its leaders did battle against one another. As the AfD sank in the polls in the spring of this year, the Social Democrats soared under the stewardship of Martin Schulz. All of a sudden, the most serious threat to Merkel came not from the right — as was the case with ruling parties across the West — but from the left.

But Schulz’s glory days were short-lived. By the early summer, his party had tumbled in the polls. Schulz never managed to land effective attacks on Merkel related to her refugee policy, namely because the Social Democrats do not have values or ideas that differ greatly from hers.

Meanwhile, the AfD has regained ground — and could be the third-largest party in the Parliament — but the far-right faction no longer threatens the dominance of the Christian Democrats. At least not this fall.

5. Germany is a country steeped in its past.

Whether or not the past — Merkel's own or that of her country — dictated her decision-making, it offered a powerful narrative encasing her refugee policy.

At a meeting in Brussels in October 2015, Merkel — the first German chancellor from the former communist east — is said to have countered Hungarian Prime Minister Viktor Orban's call for fences across Europe by offering, “I lived behind a fence for too long for me to now wish for those times to return.”

Her biographers also point to her Christian upbringing — her father was a Lutheran pastor — in accounting for her resolve.

Merkel did not cast Germany's obligation to refugees displaced by the Syrian war as a way of atoning for Nazi crimes. But the 1951 Geneva convention on refugees, which sets the terms of international responsibility, was a response to the Holocaust, a point not lost on citizens of a country still coming to grips with its dark past. This is evident in debates about the possibility of German patriotism and the existence of a unified national culture.

“I don't think there is a German 'we,' " said Jonas Hoffmann, who sells fruit in Berlin. “To be proud of Germany also means being ashamed of Hitler.”

For some, this history is a rallying cry for a liberal stance toward refugees. But it is also a point of contention, as the arrival of Muslim refugees — some of whom have hostile attitudes toward Jews — has prompted new reflections about Germany's unique obligation arising from the Holocaust.

Merkel has addressed this knotted issue. Civil society groups are working on it, too. But it's one example of the challenges that may not fell the chancellor but will probably occupy her and her successors for years to come.