TOKYO — Few foreign leaders have courted President Trump as assiduously as the prime minister of Japan, Shinzo Abe.

Since Mr. Trump’s election victory in November, Mr. Abe has been an eager guest at Trump Tower in New York and at Mar-a-Lago, the president’s Florida resort, where the two men bonded in February over golf and responded to a North Korean missile test in full view of diners.

Now, as Mr. Trump and the leader of North Korea, Kim Jong-un, exchange increasingly fiery words over the North’s nuclear weapons program, Mr. Abe’s relationship with the president is being tested. The North’s accelerating military advances — and Mr. Trump’s volatile response — could complicate Japan’s close alliance with the United States and Mr. Abe’s political future.

Mr. Abe, analysts say, has sought favor with Mr. Trump for two reasons: to blunt the president’s criticism of Japan on trade issues — a recurring theme for Mr. Trump during his run for office — and to ensure the president’s commitment to Japan’s defense.