MADRID (Reuters) - Spanish former prime minister Mariano Rajoy told a court that Catalan separatist leaders knew exactly what they were risking when they called an illegal referendum and declared independence for their region in 2017.

FILE PHOTO - Spain's Prime Minister Mariano Rajoy takes his seat at Parliament before the vote of a no confidence motion in Madrid, Spain, June 1, 2018. REUTERS/Sergio Perez

Twelve separatist leaders are on trial over the failed independence drive, facing years in jail on charges of rebellion and misuse of public funds, in a historic court case that is testing the workings of Spain’s democracy.

“The worst part is that they knew. They were completely aware that I wouldn’t accept this,” said Rajoy, who was Spain’s conservative prime minister at the time. “They created an impossible situation knowing they risked exactly this,” he said, referring to the referendum and independence proclamation.

Rajoy’s centre-right government sent thousands of anti-riot police to Catalonia where they used batons and rubber bullets to stop an independence referendum on Oct. 1, 2017.

Weeks later, Rajoy imposed direct rule on the wealthy northeastern region and called an election there hoping the independence drive would fizzle out. In the end pro-independence parties retained a slim majority in the regional assembly.

“No prime minister of any country ... can accept that someone wants to eliminate the rule of law in their own country, which is what happened,” said Rajoy, who insisted he had warned Catalonia’s leaders that he could accept neither the referendum nor the declaration of independence.

“No prime minister can look the other way when such things happen,” he said, defending his record during Spain’s biggest political crisis since dictatorship gave way to democracy in the late 1970s.

Rajoy was only the second former prime minister to testify in Spain’s highest court; former Socialist leader Felipe Gonzalez gave evidence in 1998 in a trial of paramilitary police groups accused of abuses against Basque separatists.

Rajoy, who was ousted from office last June in a no confidence motion by the Socialists over a corruption scandal in his party, was questioned about the circumstances surrounding the failed independence bid by Catalonia.

He had already become Spain’s first sitting prime minister to give evidence in court, in the summer of 2017, over a long-running graft scandal that tainted his conservative People’s Party and ultimately ended his political career.

Rajoy is one of 500 witnesses in the trial against 12 former Catalan leaders, nine of whom have been jailed and charged with rebellion.

Former Catalan deputy leader Oriol Junqueras, the most high-profile defendant in the trial, has said he is a political prisoner and insists his region has the right to secede from Spain.