Skip to content

Mocks in CobolCheck

Dave Nicolette edited this page Jan 3, 2021 · 9 revisions

Home -> User Guide -> General User Guide -> Mocks and Stubs ->

Cobol-check replaces all I/O-related Cobol statements with comments in the program under test. You can specify a mock in your test suite when you want specific behavior to occur when the program under test attempts an I/O operation, such as:

  • populating a Working-Storage area with the contents of a fake file record;
  • simulating an I/O error, such as a "file not found" condition;
  • simulating a successful write operation; or
  • counting the number of times the program under test attempted a particular I/O operation.

You may wonder how you can test against files when we stub all I/O operations. The answer is that tests that operate against files are not "unit" tests; they are functional tests of some sort, but they are at a higher level of abstraction than unit tests. See What is a Unit Test? for more information.

You don't need a special tool like cobol-check to run tests against files and databases. Unit-level checking as supported by cobol-check is a missing piece of the puzzle for Cobol, but other levels of checking are already feasible using existing tools. See Component and Integration Testing for more information.

Mocks in cobol-check

In cobol-check, stubs and mocks are the same. There is only one keyword, MOCK. There is no keyword, STUB. The conceptual difference is that a mock includes some behavior (Cobol statements) that a test case wants to happen as part of the conditions surrounding the test.

In addition, in cobol-check a mock keeps track of the number of times the code under test invokes it. Our test cases can then check whether the result is as expected, as well as whether the mocked resource has been invoked some particular number of times during test execution.

In cobol-check, we always specify MOCK even if our intent, conceptually, is to stub a resource.

So, a stub looks like this:

    MOCK FILE MY-FILE
    END-MOCK

and mocks look like this:

    MOCK FILE FIRST-FILE
        ON OPEN 
            STATUS IS FILE-NOT-FOUND
        ON CLOSE
            STATUS IS NOT-OPEN
    END-MOCK

    MOCK FILE SECOND-FILE
        ON READ
            MOVE 'AAAAABBBBBCCCCCDDDDDEEEEE' TO WS-RECORD-AREA
    END-MOCK

Clone this wiki locally