RALEIGH, N.C. (AP) — A federal judge has rejected claims by GOP leaders that North Carolina’s attorney general — a Democrat running for governor — won’t adequately defend a state law that lets magistrates refuse to perform gay marriages based on their religious beliefs.

Attorney General Roy Cooper’s role in defending laws passed by the state’s Republican-led legislature has been a thorny issue in his campaign to unseat Republican Gov. Pat McCrory — perhaps the country’s most closely watched gubernatorial race.

North Carolina has been at the center of a national debate over LGBT rights since it enacted another law this year requiring transgender people to use restrooms corresponding to the sex on their birth certificate — a measure Cooper denounced and declined to defend in court.

Cooper’s office, however, is fighting in court to uphold the 2015 law that allows local magistrates to decline to perform same-sex marriages. The case represents a political quandary for Cooper, who has spoken against that law, too.

Still, U.S. Magistrate Judge Dennis Howell wrote Thursday that Cooper has “zealously” and “aggressively” fought for the the law by seeking dismissal of plaintiffs’ claims that the measure is unconstitutional. Howell’s order denied an effort by state House Speaker Tim Moore and Senate leader Phil Berger, both Republicans, to intervene with private attorneys.

“Currently, the N.C. Department of Justice is zealously defending this case on behalf of the State,” Howell wrote, referring to Cooper’s office. The judge added that he saw “no benefit” from letting others in government positions intervene in the case using outside counsel.

Howell also denied motions to intervene by three current or former magistrates who either opted for recusal or resigned because they were unwilling to perform same-sex marriages.

In the context of the governor’s race, the judge’s validation of Cooper’s professionalism highlights a paradox: While Cooper is defending the law in court, McCrory himself vetoed the measure before the General Assembly voted to override him. The governor said he believes marriage is between a man and a woman, but government employees who take an oath to do a job shouldn’t be allowed to break those oaths.

A spokeswoman for Cooper said Friday that the judge’s order speaks for itself.

Moore and Berger cited negative public comments Cooper made about the law in arguing that they should be allowed to step in.

“There is simply no way to maintain that the Attorney General can properly discharge his responsibility of defending a statute that he has so publicly denigrated and so openly stated should not be the law in North Carolina,” their attorneys wrote in a March court filing.

Under the law, magistrates and some register of deeds officials who issue licenses can opt out of performing marriages based on a “sincerely held religious objection.” Any decision to opt out applies to all marriages — same-sex and heterosexual — for at least six months. When the lawsuit was filed late last year, only North Carolina and Utah provided such religious-objection options for court officials.

On the day the law was enacted in June 2015, Cooper said on his official Twitter account: “New magistrates law will hurt NC families, cost jobs, and spur needless, costly litigation. Another bad idea.”

When the lawsuit was filed in December, the attorney general told reporters that he personally opposed the law but would still defend the state in court.

Messages for Moore and McCrory weren’t immediately returned Friday, and a spokeswoman for Berger said he wasn’t available.