You need a global NotesDocument variable that you can bind the DocumentContext to. The reason that it has to be global is that it has probably has to be available across multiple methods/scopes. It should be put inside a script library that doesn't use other script libraries. In most of my apps, I have a script library stack, where the most general code is put inside a script library that most other script libraries use (either through "inheritance" or through Use). This is where I put the global declarations.
Then you need a function that returns either NotesSession.DocumentContext, or a temporary document. This should be put inside the same script library as the aforementioned global declaration.
contextDoc is declared as a global variable.
Function getDocumentContext() As NotesDocument
On Error GoTo returnEmptyDocument
'// Returns NotesSession.DocumentContext if availabe, else a temporary document
Dim s As New NotesSession()
Set getDocumentContext = s.documentContext
If getDocumentContext Is Nothing Then GoTo returnEmptyDocument
If contextDoc Is Nothing Then Set contextDoc = s.currentDatabase.createDocument()
Set getDocumentContext = contextDoc
Wherever in your code where you fetch NotesDocument.DocumentContext, you have to change that to call the above function, getDocumentContext().
To test code, all you have to do is to create an agent, bind getDocumentContext to a NotesDocument variable (or modify the global var), set the needed fields, and you're ready to debug the code.
Share and enjoy!
Call contextDoc.replaceItemValue( "someNeededField", "someTestValue" )