That’s more or less my current solution. I’m not using SHCMD (which I’ll definitely look into) but a User Exit for calling the shell script. However, the overhead for calling the script and “mocking” via the file system is about 1 second per call. And that would slow down my test suite. I’m thinking of hundreds of tests with these mocks and they have to run fast. So I thought a Natural internal solution would be better than messing with the file system.
Yes, that’s exactly what I want!
Changing the production code could introduce bugs, which I don’t want. Also, I don’t want the production modules to “know” that they might be tested and should behave differently in this case. They should not know anything about the mocks. And your solution would also require me to write the mocks up front, which I don’t want. I need the tests to generate and compile the mocks at runtime, because each test might need individual return values from the mocked modules. And if I have to write these mocks as “real” modules, there would be lots of those laying around.
Example: Test a subprogram TOTEST, that adds two numbers, one of which comes from a CALLNAT ‘TOMOCK’.
Module to test:
CALLNAT 'TOMOCK' #SECOND-NUMBER
#MY-PARAM := 5 + #SECOND-NUMBER
Test 1:
/* -> mock TOMOCK to return 3
CALLNAT 'TOTEST' #MY-PARAM
ASSERT-EQUALS 8 #MY-PARAM
Test 2:
/* -> mock TOMOCK to return -3
CALLNAT 'TOTEST' #MY-PARAM
ASSERT-EQUALS 2 #MY-PARAM
For this simple test I would need to create two mock modules each with only a single line of code (returning 3 and -3). And I don’t want this overhead. Instead, these simple modules should be created at runtime and should be disposed right after the test finishes.