Mock non-virtual methods

31 views
Skip to first unread message

Ana

unread,
Aug 5, 2025, 8:10:33 AMAug 5
to Spring4D
Hello,

I'm using spring4d for unit testing, and I've run into a challenge: I need to mock a method that doesn't belong to a class, such as CreateProcess, GetTempPath... from Winapi.Windows. Is there any way to mock these system-level functions using Spring4d, without changing the original code?

An example of using them:
1. function GetWinTmpPath:string;
...
GetTempPath(Requiered, Pchar(Result));
...

2. function WinExec(const cmd:string; const cmdshow: integer):cardinal;
begin
...
Result:=CreateProcess(..);
if result then ...
...
end;

Thanks in advance!

Stefan Glienke

unread,
Aug 5, 2025, 8:13:07 AMAug 5
to Spring4D
No, for such things, you need to use libraries such as DDetours.
Another possible approach would be to not directly use such functions, but put them behind an interface that you can then mock.

Ana

unread,
Aug 5, 2025, 8:34:59 AMAug 5
to Spring4D
Ok, thank you for the answer and for pointing out how to approach this!
Reply all
Reply to author
Forward
0 new messages