Frauke Petry (right), chairwoman of the anti-immigration party Alternative for Germany (AfD), smiles as she delivers a speech after first exit polls in three regional state elections at the AfD party's election night party in Berlin, March 13, 2016. At left is fellow party member Beatrix von Storch. REUTERS/Fabrizio Bensch

Torsten Heinrich was first drawn to the Alternative für Deutschland (Alternative for Germany) party in 2013, as he watched the German government spearhead a bailout of debt-ridden Greece. To Heinrich, the German-led European bailout was not only a violation of his free-market principles but an unfair burden on the hard-working German people.

The party attracted Heinrich, now 33, for its economic philosophy and its critical approach to Germany's role in the European Union, and he would go on to become the AfD's youth organizer.

“The original plan was simply sane and what anyone who is libertarian-leaning would least support,” he said of the party's platform. But Heinrich left the group because of growing ideological differences in 2014 and says he no longer recognizes the party currently surging in polls ahead of state elections Sunday.

As a continuing refugee crisis continues to trouble the German population, AfD has evolved from a euroskeptic, free-market-economics party into an anti-immigrant, far right-wing movement. The party is known for its often wild rhetoric about immigration, such as the suggestion that German police should shoot refugees they see trying to cross the border, and the party's transition reflects a nationwide shift toward right-wing populism that could pose a challenge to Chancellor Angela Merkel’s Christian Democratic Union ahead of national elections in 2017.

“The old economics sanity, the knowledge, many of them have left the party,” said Heinrich, adding, "They are increasingly going to the right, pushing the boundaries and seeing how far they can go.”

German Chancellor Angela Merkel campaigns with her Christian Democratic Union's leader in Baden-Württemberg, Guido Wolf, at a rally Wednesday in Nuertingen. State elections scheduled for Sunday in three German states will be a crucial test for Merkel, who has come under increasing pressure over her liberal immigration policy. Getty Images

More than 1 million refugees sought asylum in Germany in 2015, with the vast majority of them fleeing violent conflict in places like Iraq, Syria and Afghanistan. Germany's strong economy, along with Merkel's promise that there was no limit to the number of refugees the nation would take, attracted many asylum-seekers who had found themselves stranded on borders throughout Europe as EU nations increasingly put up fences and rejected refugee quotas.

Merkel's approach to refugee policy has received both loud applause and harsh criticism from Germans, many of whom were initially welcoming toward refugees when the chancellor announced her open-door policy in August 2015. The tide of opinion began to turn against refugees, particularly after a high-profile incident in Cologne when a group of men, some of whom were asylum-seekers, attempted to rob and sexually assault dozens of women at a New Year's Eve celebration.

State elections Sunday in the three states of Rhineland-Palatinate, Saxony-Anhalt and Baden-Württemberg will test of the strength of Merkel's Christian Democratic Union as well as its viability ahead of federal elections in 2017. Around 12 million people are expected to take to the polls to elect their regional parliaments this weekend.

AfD currently holds no parliamentary seats in the three states, but recent polling from Bild newspaper put support for the party at 12.5 percent in the CDU stronghold of Baden-Württemberg and 19 percent in Saxony-Anhalt, in the former East Germany. While the poll numbers put the party far from a majority, the surge in support has the Christian Democrats ill at ease.

"These are numbers that really hit us," said Guido Wolf, one of the leading candidates for the CDU, Agence France-Presse reported. Wolf called the race the "most difficult election campaign" for the party.

Germany isn't the only country that has seen a rise in support for far right-wing anti-immigrant politicians amid continentwide concerns both over a migration crisis and the growing threat of domestic terror. Slovakia elected several avowed neo-fascist members of Parliament in national elections last weekend, following in the steps of neighboring Hungary and Poland, where nationalist and frequently violent groups have grown their numbers in the past year.

Western Europe is not immune, either. In France, the National Front led by Marine Le Pen won six out of 13 regions in the first round of local elections in December that took place just one month after terrorists struck Paris, leaving 130 dead and many more wounded. While a last-minute alliance between the ruling parties kept the National Front from winning any of the regions in second-round voting, the anti-immigrant group's gains were unprecedented.

Bernd Lucke, former leader of Alternative für Deutschland and founding member of the euroskeptic Alliance for Progress and Renewal (Allianz für Fortschritt und Aufbruch) speaks during the latter party's founding congress in Frankfurt in November 2015. Like some other AfD founders, he quit after its turn the right. AFP/Getty Images

Founded in early 2013 by politician Alexander Gauland, economist Bernd Lucke and journalist Konrad Adam, Alternative for Germany focused its initial efforts on opposing the Merkel government’s handling of the eurozone crisis. Following Europe's economic downturn in 2008, national deficits and unemployment soared while growth stalled throughout the continent. As the largest economy in Europe, Germany led the charge of bailouts of debt-ridden Southern European countries, and the party attracted voters who believed Merkel’s policies were not in the interest of the German people.

Many of the founding members were professors and two-thirds held doctorate degrees, allowing the party to present itself as an intellectual alternative to other conservative parties that leaders accused of being too engrained in establishment European politics to look out for German interests.

As the party splintered ideologically in 2014, a more radical, populist faction led by Frauke Petry took over, pushing founders like Lucke to leave. Under the new leadership, the party focused on the migration issue throughout 2015, as economic policies were hardly mentioned and sometimes even reversed, with some candidates advocating for a minimum wage when the party used to oppose it.

“The rise of AfD is very much connected to migration and to the refugee crisis ... and it has little to do with the economic situation” said Alessio Terzi, a macroeconomist specializing in EU economy for the Brussels-based think tank Bruegel. The continuing strength of the German economy made it difficult for the party to stand on a platform focused on criticizing the establishment’s fiscal policies, Terzi pointed out.

The party has appealed to both rich and poor in Germany, moving away from its economic roots to campaign almost purely on the migration issue. The economy has not been a focus for AfD in these state elections, in part because many indicators show it is stronger than ever, particularly for job-seekers. Even with immigrant fears and the arrival of hundreds of thousands of refugees throughout 2015, national unemployment in January dropped to 6.2 percent — its lowest rate since the reunification of East and West Germany in 1990.

Right-wing populist parties often appeal to working-class people, particularly in times of economic uncertainty, but AfD has performed well in the polls even in prosperous regions, such as the town of Friedrichshafen, where the group has made gains despite the town's unemployment not budging higher than 3.4 percent in years.

Germany refugees
Asylum-seekers wait for their registration after arriving at a center for refugees in Giessen, in western Germany, Dec. 2, 2015. AFP/Getty Images

As Merkel moves closer to a deal with Europe on the migration crisis, AfD may once again find itself without a strong issue to use against the government, according to one German political analyst. The Christian Democrats will be able to more easily fend off an insurgent challenge if they can point to a strong solution on the refugee question.

“This very deal is extremely close to what Angela Merkel promised the voters last summer,” explained Timo Lochocki, a Berlin-based analyst who specializes in European politics for the German Marshall Fund of the U.S., a Washington think tank.

While the state elections are just one in a series of tests for Merkel's government ahead of general elections in 2017, Alternative for Germany will face long-term challenges of its own as it fights to shake off ties with neo-Nazi groups and run as a respectable and viable party.

“This party looks unstoppable, but its success is standing on very shaky ground,” said Lochocki. Regardless of whether AfD can win in the short-term, however, the movement of political dissatisfaction it represents will not go away soon.

“Is it a threat to the CDU in the medium or long haul? Yes,” said Lochocki.