ALEXANDRIA, Va. — After losing a seven-year legal battle, James Risen, a reporter for The New York Times, reluctantly took the witness stand in federal court here on Monday, but refused to answer any questions that could help the Justice Department identify his confidential sources.

Mr. Risen said he would not say anything to help prosecutors bolster their case against Jeffrey A. Sterling, a former C.I.A. officer who is set to go on trial soon on charges of providing classified information to Mr. Risen for his 2006 book, “State of War.”